java 如何使我的 android 对话框全屏显示?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10398665/
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 make my android dialog fullscreen?
提问by HardCoder
I already tried all things I could find (on stackoverflow and the net) but I somehow cannot set my dialog to fullscreen. It has a scrollview with a textview in it, and when there is not much text in the textview the scrollview is not fullscreen. How can I force it to be fullscreen even when there is not much text visible ?
我已经尝试了所有我能找到的东西(在 stackoverflow 和网络上),但不知何故我无法将我的对话框设置为全屏。它有一个带有文本视图的滚动视图,当文本视图中没有太多文本时,滚动视图不是全屏的。即使没有太多文本可见,我如何强制它全屏显示?
I create the dialog like this:
我创建这样的对话框:
final TextView box;
final Dialog info = new Dialog(cx);
final ScrollView scroll;
info.setContentView(R.layout.dialoginfo);
info.setTitle("Info");
info.setCancelable(true);
info.getWindow().setFlags(LayoutParams.FLAG_FULLSCREEN, LayoutParams.FLAG_FULLSCREEN);
scroll = ((ScrollView) info.findViewById(R.id.scrollviewinfo));
scroll.setPersistentDrawingCache(ScrollView.PERSISTENT_NO_CACHE);
scroll.setScrollbarFadingEnabled(false);
box = (TextView) info.findViewById(R.id.infotext);
box.setText(text);
info.show();
This is the xml:
这是xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/infolayout"
>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ImageView01"
android:layout_weight="1.0"
android:id="@+id/scrollviewinfo">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/infolayout2">
<TextView android:layout_margin="5dip" android:layout_width="fill_parent" android:layout_height="wrap_content"
android:id="@+id/infotext"
android:textSize="8sp"
android:text=""/>
</LinearLayout>
</ScrollView>
</LinearLayout>
回答by MattDavis
It looks like your ScrollView has its height set to wrap_content
, I'd first try replacing it with fill_parent
.
看起来您的 ScrollView 的高度设置为wrap_content
,我首先尝试将其替换为fill_parent
。
Here are some other resources that may help you:
以下是一些可能对您有所帮助的其他资源:
How can I get a Dialog style activity window to fill the screen?
I've never used the setFlags()
method, so this may give you the same results. Basically try replacing
我从未使用过该setFlags()
方法,因此这可能会给您相同的结果。基本上尝试更换
info.getWindow().setFlags(LayoutParams.FLAG_FULLSCREEN, LayoutParams.FLAG_FULLSCREEN);
info.getWindow().setFlags(LayoutParams.FLAG_FULLSCREEN, LayoutParams.FLAG_FULLSCREEN);
with
和
info.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
info.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
Alternatively, if you wanted more exact control over the height, you can create an instance of layout params, as the first answer here describes:
或者,如果您想更精确地控制高度,您可以创建一个布局参数的实例,如此处的第一个答案所述:
How to make an alert dialog fill 90% of screen size?
You can also use this code to get the screen size, if you wanted to modify it to be slightly smaller than full screen.
如果您想将其修改为略小于全屏,您也可以使用此代码来获取屏幕尺寸。
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
There are quite a few other ways to get the current screen size, too, this is just one example. Good luck!
还有很多其他方法可以获取当前屏幕大小,这只是一个示例。祝你好运!
回答by Gophermofur
Have you tried setting layout_height of the ScrollView to "match_parent" (and you can set the LinearLayout to "match_parent" too, though I don't think it's necessary)?. I'm guessing it shrinks when there isn't much text because it's set to "wrap_content". Try this:
您是否尝试将 ScrollView 的 layout_height 设置为“match_parent”(您也可以将 LinearLayout 设置为“match_parent”,但我认为没有必要)?。我猜当没有太多文本时它会缩小,因为它被设置为“wrap_content”。试试这个:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_below="@+id/ImageView01"
android:layout_weight="1.0"
android:id="@+id/scrollviewinfo">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/infolayout2">
...
</LinearLayout>
</ScrollView>
回答by Ayan
Try wrapping your dialog custom layoutinto RelativeLayoutinstead of Linear Layout. That worked for me.
尝试将您的对话框自定义布局包装到RelativeLayout而不是线性布局。那对我有用。