Android 如何让对话框风格的活动窗口填满屏幕?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1362723/
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 can I get a Dialog style activity window to fill the screen?
提问by Matthias
I am using an activity with the dialog theme set, and I want it to be full screen. I tried all sorts of things, even going through the WindowManager to expand the window to full width and height manually, but nothing works.
我正在使用一个带有对话框主题集的活动,我希望它是全屏的。我尝试了各种方法,甚至通过 WindowManager 手动将窗口扩展到全宽和全高,但没有任何效果。
Apparently, a dialog window (or an activity with the dialog theme) will only expand according to its contents, but even that doesn't always work. For instance, I show a progress bar circle which has width and height set to FILL_PARENT (so does its layout container), but still, the dialog wraps around the much smaller progress bar instead of filling the screen.
显然,对话窗口(或具有对话主题的活动)只会根据其内容展开,但即使如此也并不总是有效。例如,我展示了一个进度条圆圈,它的宽度和高度设置为 FILL_PARENT(它的布局容器也是如此),但对话框仍然环绕在更小的进度条周围,而不是填充屏幕。
There must be a way of displaying something small inside a dialog window but have it expand to full screen size without its content resizing as well?
必须有一种方法可以在对话窗口中显示一些小的东西,但它是否可以扩展到全屏大小而不调整其内容大小?
回答by Matthias
I found the solution:
我找到了解决方案:
In your activity which has the Theme.Dialog
style set, do this:
在具有Theme.Dialog
样式集的活动中,执行以下操作:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
}
It's important that you call Window.setLayout()
afteryou call setContentView()
, otherwise it won't work.
调用Window.setLayout()
后调用很重要setContentView()
,否则将无法工作。
回答by Serhii Nadolynskyi
You may add this values to your style android:windowMinWidthMajor and android:windowMinWidthMinor
您可以将此值添加到您的样式 android:windowMinWidthMajor 和 android:windowMinWidthMinor
<style name="Theme_Dialog" parent="android:Theme.Holo.Dialog">
...
<item name="android:windowMinWidthMajor">97%</item>
<item name="android:windowMinWidthMinor">97%</item>
</style>
回答by Padma Kumar
I just want to fill only 80% of the screen for that I did like this below
我只想填充屏幕的 80%,因为我在下面这样做了
DisplayMetrics metrics = getResources().getDisplayMetrics();
int screenWidth = (int) (metrics.widthPixels * 0.80);
setContentView(R.layout.mylayout);
getWindow().setLayout(screenWidth, LayoutParams.WRAP_CONTENT); //set below the setContentview
it works only when I put the getwindow().setLayout... line below the setContentView(..)
它仅在我将 getwindow().setLayout... 行放在 setContentView(..) 下方时才有效
thanks @Matthias
谢谢@马蒂亚斯
回答by Ayan
Wrap your dialog_custom_layout.xmlinto RelativeLayoutinstead of any other layout.That worked for me.
将您的dialog_custom_layout.xml包装到RelativeLayout而不是任何其他布局中。这对我有用。
回答by Rahul Sharma
For DialogThis may helpful for someone. I want a dialog to take full width of screen. searched a lot but nothing found useful. Finally this worked for me:
对于对话这可能对某人有帮助。我想要一个对话框来占据整个屏幕宽度。搜索了很多,但没有发现有用的。最后这对我有用:
mDialog.setContentView(R.layout.my_custom_dialog);
mDialog.getWindow().setBackgroundDrawable(null);
after adding this, my dialog appears in full width of screen.
添加后,我的对话框出现在屏幕的全宽中。
回答by HendraWD
This answer is a workaround for those who use "Theme.AppCompat.Dialog" or any other "Theme.AppCompat.Dialog" descendantslike "Theme.AppCompat.Light.Dialog", "Theme.AppCompat.DayNight.Dialog", etc. I myself has to use AppCompat dialog because i use AppCompatActivity as extends for all my activities. There will be a problem that make the dialog has padding on every sides(top, right, bottom and left) if we use the accepted answer.
对于使用“Theme.AppCompat.Dialog”或任何其他“Theme.AppCompat.Dialog”后代(如“Theme.AppCompat.Light.Dialog”、“Theme.AppCompat.DayNight.Dialog”等)的人,此答案是一种解决方法。我自己必须使用 AppCompat 对话框,因为我使用 AppCompatActivity 作为我所有活动的扩展。如果我们使用接受的答案,将会出现一个问题,使对话框的每一边(顶部、右侧、底部和左侧)都有填充。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
}
On your Activity's style, add these code
在您的 Activity 样式中,添加这些代码
<style name="DialogActivityTheme" parent="Theme.AppCompat.Dialog">
<item name="windowNoTitle">true</item>
<item name="android:windowBackground">@null</item>
</style>
As you may notice, the problem that generate padding to our dialog is "android:windowBackground", so here i make the window background to null.
正如您可能注意到的,为我们的对话框生成填充的问题是“android:windowBackground”,所以在这里我将窗口背景设为空。
回答by DYS
Matthias' answer is mostly right but it's still not filling the entire screen as it has a small padding on each side (pointed out by @Holmes). In addition to his code, we could fix this by extending Theme.Dialog style and add some attributes like this.
Matthias 的回答大部分是正确的,但它仍然没有填满整个屏幕,因为它的每一侧都有一个小填充(由@Holmes 指出)。除了他的代码之外,我们还可以通过扩展 Theme.Dialog 样式并添加一些这样的属性来解决这个问题。
<style name="MyDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
</style>
Then we simply declare Activity with theme set to MyDialog
:
然后我们简单地声明 Activity ,主题设置为MyDialog
:
<activity
android:name=".FooActivity"
android:theme="@style/MyDialog" />
回答by roy mathew
Set a minimum width at the top most layout.
在最顶部布局设置最小宽度。
android:minWidth="300dp"
For example:
例如:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="300dp">
<!-- Put remaining contents here -->
</LinearLayout>
回答by Stanojkovic
This would be helpful for someone like me. Create custom dialog style:
这对像我这样的人会有帮助。创建自定义对话框样式:
<style name="MyDialog" parent="Theme.AppCompat.Light.Dialog">
<item name="windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowIsFloating">false</item>
</style>
In AndroidManifest.xml
file set theme for wanted activity:
在AndroidManifest.xml
所需活动的文件集主题中:
<activity
android:name=".CustomDialog"
...
android:theme="@style/MyDialog"/>
That is all, no need to call methods programaticaly.
就是这样,不需要以编程方式调用方法。
回答by Makvin
In your manifest file where our activity is defined
在您定义我们活动的清单文件中
<activity
android:name=".YourPopUpActivity"
android:theme="@android:style/Theme.Holo.Dialog" >
</activity>
without action bar
没有操作栏
<activity android:name=".YourPopUpActivity"
android:theme="@android:style/Theme.Holo.Dialog.NoActionBar"/>