我可以创建和显示一个自定义警报对话框很好,但即使这样,我有android:layout_width/height=“fill_parent”在对话xml中,它只和内容一样大。

我想要的是填充整个屏幕的对话框,除了20像素的填充。 然后,作为对话框一部分的图像将自动使用fill_parent拉伸到完整的对话框大小。


当前回答

您可以使用百分比(JUST)窗口对话框宽度。

看看这个来自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>

你所需要做的就是扩展这个主题,并将“Major”和“Minor”的值改为90%而不是65%。

的问候。

其他回答

***In Kotlin You can Code like This : -*** 

fun customDialog(activity: Activity?, layout: Int): Dialog {
    val dialog = Dialog(activity!!)
    try {
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
        dialog.setCancelable(false)
        dialog.setContentView(layout)
        dialog.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        dialog.window!!.setLayout(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
        dialog.show()
    } catch (e: Exception) {

    }
    return dialog
}

下面是我的自定义对话框宽度的变体:

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)方法中。

对于对话框的高度,代码应该类似于此。

到目前为止我能想到的最简单的方法

如果你的对话框是由垂直线性布局组成的,只需添加一个“高度填充”虚拟视图,它将占据屏幕的整个高度。

例如:

<?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>

注意LinearLayout属性中的android:weightSum="1"和dummy View属性中的android:layout_weight="1"

实际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类中):

public static int getScreenWidth(Activity activity) {
   Point size = new Point();
   activity.getWindowManager().getDefaultDisplay().getSize(size);
   return size.x;
}

你必须在显示之前设置对话框的高度和宽度(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));

但是,当设备改变位置时,对话框的大小也会改变。当度量发生变化时,您可能需要自己处理。 peekDecorView,暗示在活动中的布局是正确初始化的,否则你可以使用

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int wwidth = metrics.widthPixels;

为了得到屏幕大小