回答:
このディスカッショングループの投稿のAndroidプラットフォーム開発者Dianne Hackbornによると、DialogsはWindowのトップレベルのレイアウトの幅と高さをに設定していWRAP_CONTENTます。ダイアログを大きくするには、これらのパラメーターをに設定しますMATCH_PARENT。
デモコード:
    AlertDialog.Builder adb = new AlertDialog.Builder(this);
    Dialog d = adb.setView(new View(this)).create();
    // (That new View is just there to have something inside the dialog that can grow big enough to cover the whole screen.)
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    lp.copyFrom(d.getWindow().getAttributes());
    lp.width = WindowManager.LayoutParams.MATCH_PARENT;
    lp.height = WindowManager.LayoutParams.MATCH_PARENT;
    d.show();
    d.getWindow().setAttributes(lp);
属性はダイアログが表示された後に設定されることに注意してください。システムは、いつ設定されるかについて細心の注意を払っています。(ダイアログが初めて表示されるときなどに、レイアウトエンジンがそれらを設定する必要があると思います。)
Theme.Dialogを拡張してこれを行うと、setAttributesをいつ呼び出すかについて推測ゲームをする必要がなくなります。(ダイアログに適切な明るいまたは暗いテーマ、またはHoneycomb Holoテーマを自動的に採用させるのは少し手間がかかりますが、http://developer.android.com/guide/topics/ui/themesに従って行うことができます。 html#SelectATheme)
カスタムダイアログレイアウトをのRelativeLayout代わりにラップしてみてくださいLinearLayout。それでうまくいきました。
他の人が提案したように、ダイアログウィンドウでFILL_PARENTを指定しても、(Android 4.0.4では)機能しませんでした。黒いダイアログの背景が画面全体に広がるだけだったからです。
正常に機能するのは、最小表示値を使用することですが、コード内でそれを指定して、ダイアログが画面の90%を占めるようにします。
そう:
Activity activity = ...;
AlertDialog dialog = ...;
// retrieve display dimensions
Rect displayRectangle = new Rect();
Window window = activity.getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(displayRectangle);
// inflate and adjust layout
LayoutInflater inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.your_dialog_layout, null);
layout.setMinimumWidth((int)(displayRectangle.width() * 0.9f));
layout.setMinimumHeight((int)(displayRectangle.height() * 0.9f));
dialog.setView(layout);
通常、ほとんどの場合、幅を調整するだけで十分です。
設定android:minWidthとandroid:minHeightカスタムビューのXMLインチ これらは、コンテンツサイズをラップするだけではなく、アラートを強制できます。このようなビューを使用すると、次のようになります。
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:minWidth="300dp" 
  android:minHeight="400dp">
  <ImageView
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:background="@drawable/icon"/>
</LinearLayout>
              さらに簡単にこれを行うだけです:
int width = (int)(getResources().getDisplayMetrics().widthPixels*0.90);
int height = (int)(getResources().getDisplayMetrics().heightPixels*0.90);
alertDialog.getWindow().setLayout(width, height);
              onCreate、デバイスのローテーション後にダイアログを再作成しようとするシナリオに非常に役立ちました。この場合、レイアウトがまだ作成されていないため、レイアウトの幅/高さに依存することはできません。ただし、デバイスの物理的な幅/高さは引き続き使用できます。
                    ViewGroup.LayoutParams.WRAP_CONTENTは、パラメーターの1つとして渡します
                    dialog.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
              FILL_PARENT現在のディスプレイの幅の90%として計算された幅に置き換えることができます。しかし、他の答えよりも簡単なので、私は答えが好きです。
                    ここでの他のすべての答えは理にかなっていますが、ファビアンのニーズを満たしていませんでした。これが私の解決策です。それは完璧な解決策ではないかもしれませんが、私にとってはうまくいきます。全画面にあるダイアログが表示されますが、上下左右にパディングを指定できます。
まずこれをres / values / styles.xmlに入れます:
<style name="CustomDialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@color/Black0Percent</item>
    <item name="android:paddingTop">20dp</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:backgroundDimEnabled">false</item>
    <item name="android:windowIsFloating">false</item>
</style>
ご覧のとおり、android:paddingTop = 20dpが基本的に必要です。アンドロイド:windowBackground = @色/ Black0Percentはちょうどカラーコードは、私のcolor.xmlに宣言されています
res / values / color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="Black0Percent">#00000000</color>
</resources>
その色コードは、ダイアログのデフォルトのウィンドウ背景を0%の透明色で置き換えるダミーとして機能します。
次に、カスタムダイアログレイアウトres / layout / dialog.xmlをビルドします。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dialoglayout"
    android:layout_width="match_parent"
    android:background="@drawable/DesiredImageBackground"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <EditText
        android:id="@+id/edittext1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:textSize="18dp" />
    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Dummy Button"
        android:textSize="18dp" />
</LinearLayout>
最後に、dialog.xmlを使用するカスタムビューを設定するダイアログがあります。
Dialog customDialog;
LayoutInflater inflater = (LayoutInflater) getLayoutInflater();
View customView = inflater.inflate(R.layout.dialog, null);
// Build the dialog
customDialog = new Dialog(this, R.style.CustomDialog);
customDialog.setContentView(customView);
customDialog.show();
結論: CustomDialogという名前のstyles.xmlでダイアログのテーマをオーバーライドしようとしました。これは、ダイアログウィンドウのレイアウトを上書きし、パディングを設定して背景の不透明度を変更する機会を与えます。それは完璧な解決策ではないかもしれませんが、私はそれがあなたを助けることを願っています。
DialogFragment何も機能しませんDialog customDialog = new Dialog(context, STYLE);でした。カスタムダイアログ(拡張)のすべての魔法は、そうしたときに発生します。感謝、本当にありがとう。
                    (ちょうど)ウィンドウダイアログの幅にパーセンテージを使用できます。
Holo Themeの次の例を見てください。
<style name="Theme.Holo.Dialog.NoActionBar.MinWidth">
    <item name="android:windowMinWidthMajor">@android:dimen/dialog_min_width_major</item>
    <item name="android:windowMinWidthMinor">@android:dimen/dialog_min_width_minor</item>
</style>
 <!-- The platform's desired minimum size for a dialog's width when it
     is along the major axis (that is the screen is landscape).  This may
     be either a fraction or a dimension. -->
<item type="dimen" name="dialog_min_width_major">65%</item>
このテーマを拡張して、「メジャー」と「マイナー」の値を65%ではなく90%に変更するだけです。
よろしく。
以下は私にとってうまくいきました:
    <style name="MyAlertDialogTheme" parent="Base.Theme.AppCompat.Light.Dialog.Alert">
        <item name="windowFixedWidthMajor">90%</item>
        <item name="windowFixedWidthMinor">90%</item>
    </style>
(注:以前の回答で提案されているwindowMinWidthMajor / Minorではうまくいきませんでした。コンテンツに応じてダイアログのサイズが変わり続けました)
その後:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.MyAlertDialogTheme);
              実際の90%計算によるソリューション:
@Override public void onStart() {
   Dialog dialog = getDialog();
   if (dialog != null) {
     dialog.getWindow()
        .setLayout((int) (getScreenWidth(getActivity()) * .9), ViewGroup.LayoutParams.MATCH_PARENT);
   }
}
どこgetScreenWidth(Activity activity)以下(Utilsのクラスで最高のPUT)で定義されています。
public static int getScreenWidth(Activity activity) {
   Point size = new Point();
   activity.getWindowManager().getDefaultDisplay().getSize(size);
   return size.x;
}
              デバイスの幅を取得します。
public static int getWidth(Context context) {
    DisplayMetrics displayMetrics = new DisplayMetrics();
    WindowManager windowmanager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    windowmanager.getDefaultDisplay().getMetrics(displayMetrics);
    return displayMetrics.widthPixels;
}
次に、それをデバイスの90%のダイアログに使用します。
Dialog filterDialog = new Dialog(context, R.style.searchsdk_FilterDialog);
filterDialog.setContentView(R.layout.searchsdk_filter_popup);
initFilterDialog(filterDialog);
filterDialog.setCancelable(true);
filterDialog.getWindow().setLayout(((getWidth(context) / 100) * 90), LinearLayout.LayoutParams.MATCH_PARENT);
filterDialog.getWindow().setGravity(Gravity.END);
filterDialog.show();
              私が考えることができる最も簡単な方法-
ダイアログが垂直のLinearLayoutで作成されている場合は、「高さを埋める」ダミービューを追加するだけで、画面の高さ全体を占めます。
例えば -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:weightSum="1">
    <EditText
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:id="@+id/editSearch" />
    <ListView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:id="@+id/listView"/>
   <!-- this is a dummy view that will make sure the dialog is highest -->
   <View
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_weight="1"/>
</LinearLayout>
お知らせandroid:weightSum="1"のLinearLayoutの属性とではandroid:layout_weight="1"ダミーで閲覧者の属性
これを表示するには、ダイアログの高さと幅を設定する必要があります(dialog.show())
だから、このようなことをしてください:
dialog.getWindow().setLayout(width, height);
//then
dialog.show()
このコードを取得して、いくつか変更を加えました。
dialog.getWindow().setLayout((int)(MapGeaGtaxiActivity.this.getWindow().peekDecorView().getWidth()*0.9),(int) (MapGeaGtaxiActivity.this.getWindow().peekDecorView().getHeight()*0.9));
ただし、デバイスが位置を変更すると、ダイアログのサイズが変わる可能性があります。メトリックが変化した場合は、おそらく自分で処理する必要があります。PD:peekDecorViewは、アクティビティのレイアウトが適切に初期化されていることを意味します。
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int wwidth = metrics.widthPixels;
画面サイズを取得するために
ダイアログオブジェクトを初期化し、コンテンツビューを設定した後。これをして楽しんでください。
(幅を90%、ツールバーを超えるため、70%を高さに設定している場合)
DisplayMetrics displaymetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int width = (int) ((int)displaymetrics.widthPixels * 0.9);
int height = (int) ((int)displaymetrics.heightPixels * 0.7);
d.getWindow().setLayout(width,height);
d.show();
              私の答えはコマのに基づいていますが、onStartをオーバーライドする必要はありませんが、新しいフラグメントを作成するときにデフォルトでほとんど常にオーバーライドされるonCreateViewのみをオーバーライドする必要があります。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.your_fragment_layout, container);
    Rect displayRectangle = new Rect();
    Window window = getDialog().getWindow();
    window.getDecorView().getWindowVisibleDisplayFrame(displayRectangle);
    v.setMinimumWidth((int)(displayRectangle.width() * 0.9f));
    v.setMinimumHeight((int)(displayRectangle.height() * 0.9f));
    return v;
}
Android 5.0.1でテストしました。
カスタムダイアログの幅のバリエーションは次のとおりです。
DisplayMetrics displaymetrics = new DisplayMetrics();
mActivity.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int width = (int) (displaymetrics.widthPixels * (ThemeHelper.isPortrait(mContext) ? 0.95 : 0.65));
WindowManager.LayoutParams params = getWindow().getAttributes();
params.width = width;
getWindow().setAttributes(params);
したがって、デバイスの向きによって(ThemeHelper.isPortrait(mContext))にダイアログの幅は95%(縦モード)または65%(横向き)になります。作者が尋ねたのはもう少しですが、誰かに役立つかもしれません。
Dialogから拡張するクラスを作成し、このコードをonCreate(Bundle savedInstanceState)メソッドに配置する必要があります。
ダイアログの高さの場合、コードはこれに似ています。
ThemeHelper?私のプロジェクトではこのクラスを利用できません。
                    ThemeHelper私のニーズのためのヘルパークラスです。そのメソッドisPortrait(Context)は、デバイスの画面の向きが縦向きか横向きかを返します。
                    public static WindowManager.LayoutParams setDialogLayoutParams(Activity activity, Dialog dialog)
    {
        try 
        {
            Display display = activity.getWindowManager().getDefaultDisplay();
            Point screenSize = new Point();
            display.getSize(screenSize);
            int width = screenSize.x;
            WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
            layoutParams.copyFrom(dialog.getWindow().getAttributes());
            layoutParams.width = (int) (width - (width * 0.07) ); 
            layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
            return layoutParams;
        } 
        catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }
              上記の答えの多くは良いですが、どれもうまくいきませんでした。@nmrからの回答を組み合わせて、これを取得しました。
final Dialog d = new Dialog(getActivity());
        //  d.getWindow().setBackgroundDrawable(R.color.action_bar_bg);
        d.requestWindowFeature(Window.FEATURE_NO_TITLE);
        d.setContentView(R.layout.dialog_box_shipment_detail);
        WindowManager wm = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE); // for activity use context instead of getActivity()
        Display display = wm.getDefaultDisplay(); // getting the screen size of device
        Point size = new Point();
        display.getSize(size);
        int width = size.x - 20;  // Set your heights
        int height = size.y - 80; // set your widths
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.copyFrom(d.getWindow().getAttributes());
        lp.width = width;
        lp.height = height;
        d.getWindow().setAttributes(lp);
        d.show();
                  ...
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    Dialog d = builder.create(); //create Dialog
    d.show(); //first show
    DisplayMetrics metrics = new DisplayMetrics(); //get metrics of screen
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int height = (int) (metrics.heightPixels*0.9); //set height to 90% of total
    int width = (int) (metrics.widthPixels*0.9); //set width to 90% of total
    d.getWindow().setLayout(width, height); //set layout
              カスタマイズ可能なダイアログを表示するには、CustomDialogなどのスタイル@ style.xmlを使用する必要があります。
<style name="CustomDialog" parent="@android:style/Theme.DeviceDefault.Light.Dialog">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@color/colorWhite</item>
        <item name="android:editTextColor">@color/colorBlack</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    </style>
このスタイルをActivity.javaで次のように使用します
Dialog dialog= new Dialog(Activity.this, R.style.CustomDialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.custom_dialog);
そしてcustom_dialog.xmlはあなたのレイアウトディレクトリ内にあるはずです
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="10dp"
    android:paddingRight="10dp">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="20dp"
        android:id="@+id/tittle_text_view"
        android:textColor="@color/colorBlack"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="10dp"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="20dp"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="20dp"
        android:layout_marginRight="20dp">
        <EditText
            android:id="@+id/edit_text_first"
            android:layout_width="50dp"
            android:layout_height="match_parent"
            android:hint="0"
            android:inputType="number" />
        <TextView
            android:id="@+id/text_view_first"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:gravity="center"/>
        <EditText
            android:id="@+id/edit_text_second"
            android:layout_width="50dp"
            android:layout_height="match_parent"
            android:hint="0"
            android:layout_marginLeft="5dp"
            android:inputType="number" />
        <TextView
            android:id="@+id/text_view_second"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:gravity="center"/>
    </LinearLayout>
</LinearLayout>
              Constraint Layoutを使用している場合は、その内部に任意のビューを設定して、画面の一部を次のように埋めることができます。
layout_constraintWidth_percent = "0.8"
たとえば、ダイアログ内にScrollViewがあり、それを画面の高さのパーセンテージに設定したい場合などです。それは次のようになります:
<ScrollView
            android:id="@+id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintHeight_percent="0.8">
それが誰かを助けることを願っています!!
これは私のために働いた短い答えです(API 8とAPI 19でテスト済み)。
Dialog mDialog;
View   mDialogView;
...
// Get height
int height = mDialog.getWindow()
.getWindowManager().getDefaultDisplay()
.getHeight();
// Set your desired padding (here 90%)
int padding = height - (int)(height*0.9f);
// Apply it to the Dialog
mDialogView.setPadding(
// padding left
0,
// padding top (90%)
padding, 
// padding right
0, 
// padding bottom (90%)
padding);
                  final AlertDialog alertDialog;
    LayoutInflater li = LayoutInflater.from(mActivity);
    final View promptsView = li.inflate(R.layout.layout_dialog_select_time, null);
    RecyclerView recyclerViewTime;
    RippleButton buttonDone;
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mActivity);
    alertDialogBuilder.setView(promptsView);
    // create alert dialog
    alertDialog = alertDialogBuilder.create();
    /**
     * setting up window design
     */
    alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    alertDialog.show();
    DisplayMetrics metrics = new DisplayMetrics(); //get metrics of screen
    mActivity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int height = (int) (metrics.heightPixels * 0.9); //set height to 90% of total
    int width = (int) (metrics.widthPixels * 0.9); //set width to 90% of total
    alertDialog.getWindow().setLayout(width, height); //set layout
    recyclerViewTime = promptsView.findViewById(R.id.recyclerViewTime);
    DialogSelectTimeAdapter dialogSelectTimeAdapter = new DialogSelectTimeAdapter(this);
    RecyclerView.LayoutManager linearLayoutManager = new LinearLayoutManager(this);
    recyclerViewTime.setLayoutManager(linearLayoutManager);
    recyclerViewTime.setAdapter(dialogSelectTimeAdapter);
    buttonDone = promptsView.findViewById(R.id.buttonDone);
    buttonDone.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            alertDialog.dismiss();
        }
    });