如何使一个警告对话框填充屏幕尺寸的90%?

我可以创build并显示一个自定义的警报对话框,但即使如此,我也有android:layout_width/height="fill_parent"在对话框中的xml它只是与内容一样大。

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

根据Android平台开发人员Dianne Hackborn在本次讨论组中的WRAP_CONTENT ,Dialogs将其顶层布局的宽度和高度设置为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的猜谜游戏。 (尽pipe让对话框自动采用适当的明亮或黑暗的主题,或者Honeycomb Holo主题还有一些工作要做,这可以根据http://developer.android.com/guide/topics/ui/themes完成。; html#SelectATheme )

在对话窗口中指定FILL_PARENT,就像其他人所build议的那样,对我来说(在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); 

一般来说,在大多数情况下只需要调整宽度即可。

在你的自定义视图xml中设置android:minWidthandroid:minHeight 。 这些可以强制警告不要只包装内容大小。 使用这样的视图应该这样做:

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

尝试将自定义对话框布局封装到RelativeLayout而不是LinearLayout 。 这对我有效。

 dialog.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 

更简单的做到这一点:

 int width = (int)(getResources().getDisplayMetrics().widthPixels*0.90); int height = (int)(getResources().getDisplayMetrics().heightPixels*0.90); alertDialog.getWindow().setLayout(width, height); 

所有其他的答案在这里是有道理的,但它不符合法比安需要的。 这是我的解决scheme。 这可能不是一个完美的解决scheme,但它适用于我。 它显示了一个全屏的对话框,但是您可以在顶部,底部,左侧或右侧指定填充。

首先把这个放在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基本上是你需要的。 android:windowBackground = @ color / Black0Percent只是在我的color.xml中声明的颜色代码

RES /值/ color.xml

 <?xml version="1.0" encoding="utf-8"?> <resources> <color name="Black0Percent">#00000000</color> </resources> 

该颜色代码只是作为一个虚拟,用0%的透明色replace对话框的默认窗口背景。

接下来构build自定义对话框布局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中覆盖对话框的主题。 它覆盖了对话窗口的布局,并给了我设置填充和改变背景不透明度的机会。 它可能不是完美的解决scheme,但我希望它可以帮助你.. 🙂

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

从Holo主题看这个例子:

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

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

问候。

实际90%计算的解决scheme:

 @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() 

以下工作适合我:

  <style name="MyAlertDialogTheme" parent="Base.Theme.AppCompat.Light.Dialog.Alert"> <item name="windowFixedWidthMajor">90%</item> <item name="windowFixedWidthMinor">90%</item> </style> 

(注意:windowMinWidthMajor / Minor如前面的答案build议并没有这样做,我的对话框根据内容不断变化大小)

接着:

 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.MyAlertDialogTheme); 

迄今为止我能想到的最简单的方法 –

如果你的对话框是由一个垂直的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> 

请注意LinearLayout属性中的android:layout_weight="1" ,以及虚拟View属性中的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; 

为了获得屏幕大小

我的答案是基于koma的,但它不需要重写onStart,但只有在创build新片段时,默认情况下几乎总是被覆盖的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上testing过了。

以上的答案很好,但没有一个完全为我工作。 所以我结合@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(); 

初始化对话框对象并设置内容视图。 做到这一点,享受。

(如果我设置宽度为90%,高度为70%,因为宽度为90%,它将在工具栏上)

 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(); 

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

 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%(横向)。 作者提出的问题稍多一点,但对某人可能有用。

您需要创build一个从Dialog扩展的类,并将这些代码放入onCreate(Bundle savedInstanceState)方法中。

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

 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; } } 

这里是一个简短的答案,为我工作(testingAPI 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); 
  ... 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 
 dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.WRAP_CONTENT);