如何使警报对话框填充90%的屏幕大小?

Fab*_*ian 282 android dialog

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

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

nmr*_*nmr 347

根据Android平台开发人员Dianne Hackborn在这个讨论组帖子中,Dialogs将他们Window的顶级布局宽度和高度设置为WRAP_CONTENT.要使Dialog更大,可以将这些参数设置为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);
Run Code Online (Sandbox Code Playgroud)

请注意,在显示对话框后设置属性.系统在设置时很挑剔.(我猜布局引擎必须在第一次显示对话框时设置它们.)

最好通过扩展Theme.Dialog来做到这一点,然后你就不必玩一个关于何时调用setAttributes的猜谜游戏.(尽管让对话框自动采用适当的浅色或深色主题或Honeycomb Holo主题还有一些工作要做.这可以根据http://developer.android.com/guide/topics/ui/themes来完成. html#SelectATheme)

  • 我不知道这是如何回答这个问题的?@nmr建议的代码是否使Dialog占据屏幕的90%?因为我没有看到它.对话框刚刚设置为fill_width,那么100%,而不是90%,对吧? (19认同)
  • 只需调用d.getWindow().getAttributes().width = WindowManager.LayoutParams.FILL_PARENT; 在调用show()之前会做的伎俩.此外,由于对话框的背景变得不透明,我在给定方法上遇到了一些问题.一旦删除了copyFrom((),问题就自行修复了. (8认同)
  • 我是否理解答案,或者对我不起作用. (5认同)
  • 对话框的wrap_content范围似乎不正确.事情变得更加狭窄和切断.对话框似乎设置为某个最大预定宽度. (4认同)
  • @Ted,AlertDialog使用的主题有一个保证金.因此结果没有到达屏幕的边缘,它填满了大约90%.(它绝对不是90%,仅仅是90%的美学近似值) (4认同)

小智 162

尝试将自定义对话框布局包装成RelativeLayout而不是LinearLayout.这对我有用.

  • 这是2019年,仍然有效。干杯! 确实应该是公认的答案。 (6认同)
  • @cvarsendan 你能解释一下答案吗?这对我有用。谢谢你! (3认同)
  • 在横向方向上不起作用 (3认同)
  • 所有这些技巧中,这对我有用! (2认同)
  • 对我来说也是如此。谁能解释原因! (2认同)
  • 现在已经 2023 年了,它仍然有效!格栅! (2认同)

小智 80

像对方建议的那样在对话框窗口上指定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);
Run Code Online (Sandbox Code Playgroud)

通常,在大多数情况下仅调整宽度应该是足够的.


jqp*_*liq 78

设置android:minWidthandroid: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>
Run Code Online (Sandbox Code Playgroud)

  • 什么,为什么这个答案被多次投票?这是一个糟糕的解决方案,因为它完全不考虑用户使用的设备大小. (17认同)
  • 嗯,从decoumentation我理解dp的概念.但如果屏幕比例为16:9或4:3怎么办?这个dp会不均匀拉伸? (5认同)
  • I密度独立像素总是按比例缩放,因此任何屏幕都是320x480dp,因此300x460dp视图在所有情况下都是20dp填充.或者你可以找到这样的屏幕大小:显示display =((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); int width = display.getWidth(); 并相应地设置maxWidth和maxHeight. (4认同)
  • 是的,我可以将它设置为特定的大小,但我希望它是屏幕大小减去例如20像素的填充.或宽度=屏幕宽度的90%. (2认同)
  • 不,如果屏幕比例为2:3,则dp分辨率仅为320x480,因此此方法无效. (2认同)

FOM*_*per 58

更简单就是这样做:

int width = (int)(getResources().getDisplayMetrics().widthPixels*0.90);
int height = (int)(getResources().getDisplayMetrics().heightPixels*0.90);

alertDialog.getWindow().setLayout(width, height);
Run Code Online (Sandbox Code Playgroud)

  • 如果你只想改变一个维度,传递`ViewGroup.LayoutParams.WRAP_CONTENT`作为参数之一 (12认同)

小智 50

dialog.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
Run Code Online (Sandbox Code Playgroud)

  • 但90%与100%不同.FILL_PARENT表示填充整个宽度,100%,而不是90%.对? (8认同)

ica*_*uds 25

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

首先将它放在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>
Run Code Online (Sandbox Code Playgroud)

你可以看到我有android:paddingTop = 20dp基本上是你需要的.该机器人:windowBackground = @彩/ Black0Percent只是我color.xml声明的颜色代码

RES /值/ color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="Black0Percent">#00000000</color>
</resources>
Run Code Online (Sandbox Code Playgroud)

该Color代码仅用作虚拟对象,用0%透明度颜色替换Dialog的默认窗口背景.

接下来构建自定义对话框布局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>
Run Code Online (Sandbox Code Playgroud)

最后,我们的对话框设置了使用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();
Run Code Online (Sandbox Code Playgroud)

结论:我试图在名为CustomDialog的styles.xml中覆盖对话框的主题.它会覆盖Dialog窗口布局,让我有机会设置填充并更改背景的不透明度.它可能不是完美的解决方案,但我希望它可以帮助你.. :)


Joa*_*zzi 22

您可以使用百分比(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>
Run Code Online (Sandbox Code Playgroud)

您需要做的就是扩展此主题并将"Major"和"Minor"的值更改为90%而不是65%.

问候.


Del*_*iom 19

以下工作对我来说很好:

    <style name="MyAlertDialogTheme" parent="Base.Theme.AppCompat.Light.Dialog.Alert">
        <item name="windowFixedWidthMajor">90%</item>
        <item name="windowFixedWidthMinor">90%</item>
    </style>
Run Code Online (Sandbox Code Playgroud)

(注意:在之前的答案中建议的windowMinWidthMajor/Minor没有这个技巧.我的对话框根据内容不断改变大小)

然后:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.MyAlertDialogTheme);
Run Code Online (Sandbox Code Playgroud)

  • 到目前为止最佳答案.为我工作完美! (2认同)

Meh*_*t K 17

实际90%计算的解决方案:

@Override public void onStart() {
   Dialog dialog = getDialog();
   if (dialog != null) {
     dialog.getWindow()
        .setLayout((int) (getScreenWidth(getActivity()) * .9), ViewGroup.LayoutParams.MATCH_PARENT);
   }
}
Run Code Online (Sandbox Code Playgroud)

where getScreenWidth(Activity activity)定义如下(最好放在Utils类中):

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


小智 8

获取设备宽度:

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;
}
Run Code Online (Sandbox Code Playgroud)

然后使用它来使对话框占设备的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();
Run Code Online (Sandbox Code Playgroud)


sho*_*uro 7

好吧,你必须先设置对话框的高度和宽度才能显示(dialog.show())

所以,做这样的事情:

dialog.getWindow().setLayout(width, height);

//then

dialog.show()
Run Code Online (Sandbox Code Playgroud)

  • 不为我设定尺寸。API &gt;= 21。 (2认同)

小智 6

好吧,您必须在显示此之前设置对话框的高度和宽度( dialog.show() )

所以,做这样的事情:

dialog.getWindow().setLayout(width, height);

//then

dialog.show()
Run Code Online (Sandbox Code Playgroud)

获取此代码,我对其进行了一些更改:

dialog.getWindow().setLayout((int)(MapGeaGtaxiActivity.this.getWindow().peekDecorView().getWidth()*0.9),(int) (MapGeaGtaxiActivity.this.getWindow().peekDecorView().getHeight()*0.9));
Run Code Online (Sandbox Code Playgroud)

但是,当设备改变其位置时,对话框的大小可能会改变。当指标发生变化时,也许您需要自己处理。PD: peekDecorView, 表示活动中的布局已正确初始化,否则您可以使用

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int wwidth = metrics.widthPixels;
Run Code Online (Sandbox Code Playgroud)

为了获得屏幕尺寸


Ron*_*ler 6

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

如果您的对话框是由垂直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>
Run Code Online (Sandbox Code Playgroud)

请注意android:weightSum="1"LinearLayout的属性和android:layout_weight="1"虚拟View的属性


小智 6

初始化对话框对象并设置内容视图后。这样做并享受。

(在我将 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();
Run Code Online (Sandbox Code Playgroud)


小智 6

***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
}
Run Code Online (Sandbox Code Playgroud)


kar*_*unn 5

只需给 AlertDialog 这个主题

<style name="DialogTheme" parent="Theme.MaterialComponents.Light.Dialog.MinWidth">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="android:windowMinWidthMajor">90%</item>
    <item name="android:windowMinWidthMinor">90%</item>
</style>
Run Code Online (Sandbox Code Playgroud)