Android 如何使警报对话框填充屏幕大小的 90%?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2306503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make an alert dialog fill 90% of screen size?
提问by Fabian
I can create and display a custom alert dialog just fine but even so I have android:layout_width/height="fill_parent"
in the dialog xml it is only as big as the contents.
我可以很好地创建和显示自定义警报对话框,但即使如此,我android:layout_width/height="fill_parent"
在对话框 xml 中也只与内容一样大。
What I want is dialog that fills the entire screen except maybe a padding of 20 pixel. Then the image that is part of the dialog would automatically stretch to the full dialog size with fill_parent.
我想要的是填充整个屏幕的对话框,除了可能是 20 像素的填充。然后作为对话框一部分的图像将使用 fill_parent 自动拉伸到完整的对话框大小。
回答by nmr
According to Android platform developer Dianne Hackborn in thisdiscussion group post, Dialogs set their Window's top level layout width and height to WRAP_CONTENT
. To make the Dialog bigger, you can set those parameters to MATCH_PARENT
.
根据 Android 平台开发人员 Dianne Hackborn 在此讨论组帖子中的说法,Dialogs 将其 Window 的顶级布局宽度和高度设置为WRAP_CONTENT
. 要使对话框更大,您可以将这些参数设置为MATCH_PARENT
.
Demo code:
演示代码:
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);
Note that the attributes are set after the Dialog is shown. The system is finicky about when they are set. (I guess that the layout engine must set them the first time the dialog is shown, or something.)
请注意,属性是在显示对话框之后设置的。该系统对何时设置很挑剔。(我猜布局引擎必须在第一次显示对话框时设置它们,或者其他什么。)
It would be better to do this by extending Theme.Dialog, then you wouldn't have to play a guessing game about when to call setAttributes. (Although it's a bit more work to have the dialog automatically adopt an appropriate light or dark theme, or the Honeycomb Holo theme. That can be done according to http://developer.android.com/guide/topics/ui/themes.html#SelectATheme)
最好通过扩展 Theme.Dialog 来做到这一点,这样您就不必玩关于何时调用 setAttributes 的猜谜游戏了。(尽管让对话框自动采用适当的明暗主题或 Honeycomb Holo 主题需要做更多的工作。这可以根据http://developer.android.com/guide/topics/ui/themes完成。 html#SelectATheme)
回答by cvarsendan
Try wrapping your custom dialog layout into RelativeLayout
instead of LinearLayout
. That worked for me.
尝试将您的自定义对话框布局包装到RelativeLayout
而不是LinearLayout
. 那对我有用。
回答by koma
Specifying FILL_PARENT on the dialog window, like others suggested, did not work for me (on Android 4.0.4), because it just stretched the black dialog background to fill the whole screen.
像其他人建议的那样,在对话框窗口上指定 FILL_PARENT 对我不起作用(在 Android 4.0.4 上),因为它只是拉伸黑色对话框背景以填充整个屏幕。
What works fine is using the minimum display value, but specifying it within the code, so that the dialog takes 90% of the screen.
工作正常的是使用最小显示值,但在代码中指定它,以便对话框占据屏幕的 90%。
So:
所以:
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);
In general only adjusting the width should be sufficient in most cases.
一般来说,在大多数情况下只调整宽度就足够了。
回答by jqpubliq
Set android:minWidth
and android:minHeight
in your custom view xml. These can force the alert not to just wrap content size.
Using a view like this should do it:
在您的自定义视图 xml 中设置android:minWidth
和android: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>
回答by FOMDeveloper
Even simpler just do this:
更简单的就是这样做:
int width = (int)(getResources().getDisplayMetrics().widthPixels*0.90);
int height = (int)(getResources().getDisplayMetrics().heightPixels*0.90);
alertDialog.getWindow().setLayout(width, height);
回答by Elvis
dialog.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
回答by icaneatclouds
All of the other answers here makes sense, but it did not meet what Fabian needs. Here is a solution of mine. It may not be the perfect solution but it works for me. It shows a dialog which is on fullscreen but you can specify a padding on top, bottom, left or right.
这里的所有其他答案都是有道理的,但它没有满足 Fabian 的需求。这是我的一个解决方案。它可能不是完美的解决方案,但对我有用。它显示一个全屏对话框,但您可以指定顶部、底部、左侧或右侧的填充。
First put this in your res/values/styles.xml :
首先把它放在你的 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>
As you can see I have there android:paddingTop= 20dpis basically what you need. The android:windowBackground = @color/Black0Percentis just a color code declared on my color.xml
正如你所看到的,我有android:paddingTop= 20dp基本上是你需要的。该机器人:windowBackground = @彩/ Black0Percent是我color.xml宣布只是一个颜色代码
res/values/color.xml
res/values/color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="Black0Percent">#00000000</color>
</resources>
That Color code just serves as a dummy to replace the default window background of the Dialog with a 0% transparency color.
该颜色代码仅用作虚拟对象,用 0% 透明度颜色替换对话框的默认窗口背景。
Next build the custom dialog layout res/layout/dialog.xml
接下来构建自定义对话框布局 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>
Finally here is our dialog that set custom view which uses our dialog.xml:
最后这是我们的对话框,它设置了使用我们的 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();
Conclusion:I tried to override the dialog's theme in the styles.xml named CustomDialog. It overrides the Dialog window layout and gives me the chance to set a padding and change the opacity of the background. It may not be the perfect solution but I hope it helps you..:)
结论:我试图在名为 CustomDialog 的 style.xml 中覆盖对话框的主题。它覆盖了对话框窗口布局,让我有机会设置填充和更改背景的不透明度。这可能不是完美的解决方案,但我希望它可以帮助您..:)
回答by Joao Gavazzi
You can use percentage for (JUST) windows dialog width.
您可以对(仅)窗口对话框宽度使用百分比。
Look into this example from Holo Theme:
查看 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>
All you need to do is extend this theme and change the values for "Major" and "Minor" to 90% instead 65%.
您需要做的就是扩展此主题并将“主要”和“次要”的值更改为 90% 而不是 65%。
Regards.
问候。
回答by Deliriom
The following worked fine for me:
以下对我来说很好:
<style name="MyAlertDialogTheme" parent="Base.Theme.AppCompat.Light.Dialog.Alert">
<item name="windowFixedWidthMajor">90%</item>
<item name="windowFixedWidthMinor">90%</item>
</style>
(note: windowMinWidthMajor/Minor as suggested in previous answers didn't do the trick. My dialogs kept changing sizes depending on the content)
(注意:之前答案中建议的 windowMinWidthMajor/Minor 没有解决问题。我的对话框根据内容不断改变大小)
and then:
进而:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.MyAlertDialogTheme);
回答by Mehmet K
Solution with actual 90% calculation:
实际 90% 计算的解决方案:
@Override public void onStart() {
Dialog dialog = getDialog();
if (dialog != null) {
dialog.getWindow()
.setLayout((int) (getScreenWidth(getActivity()) * .9), ViewGroup.LayoutParams.MATCH_PARENT);
}
}
where getScreenWidth(Activity activity)
is defined the following (best put in a Utils class):
其中getScreenWidth(Activity activity)
定义了以下内容(最好放在 Utils 类中):
public static int getScreenWidth(Activity activity) {
Point size = new Point();
activity.getWindowManager().getDefaultDisplay().getSize(size);
return size.x;
}