Android 对话框宽度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2634850/
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
Android dialog width
提问by Solid
I can't seem to control the dialog width. I have a simple layout like so`
我似乎无法控制对话框宽度。我有一个像这样的简单布局`
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:theme="@android:style/Theme.Dialog">
<ScrollView
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/name_prompt_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/name_prompt"
android:padding="10dip"/>
<EditText
android:id="@+id/name_inp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:lines="1"
android:maxLines="1"
android:maxLength="48"
android:inputType="text" />
<TextView
android:id="@+id/t1_prompt_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/t1_prompt"
android:padding="10dip"/>
<Spinner
android:id="@+id/t1_inp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:lines="1"
android:maxLines="1"
android:maxLength="48"
android:inputType="text"
android:singleLine="true"
android:layout_weight="1"
android:entries= "@array/t1_allowed_values" />
</LinearLayout>
</ScrollView>
</LinearLayout>
for some reason the dialog is only wide enough for the text input field about 11 chars wide. How do I make the dialog width fill the screen?
出于某种原因,对话框的宽度仅足以容纳大约 11 个字符宽的文本输入字段。如何使对话框宽度填满屏幕?
回答by UMAR
I had the same problem.
我有同样的问题。
I used following code to make dialog fill_parent and it worked fine.
我使用以下代码制作对话框 fill_parent 并且效果很好。
public class SharePost extends Dialog
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.adaptor_contentsharepost);
LayoutParams params = getWindow().getAttributes();
params.height = LayoutParams.FILL_PARENT;
getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
}
}
layout
布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/dialogWidth"
android:layout_width="match_parent"
android:layout_height="match_parent">
contents here
</LinearLayout>
回答by hrules6872
I'm using:
我正在使用:
getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
回答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 Alex Pretzlav
As Matthias points at out How can I get a Dialog style activity window to fill the screen?UMAR's solution works, but only if the window attributes are set AFTER setContentView() is called.
正如马蒂亚斯所指出的,如何让对话框风格的活动窗口填满屏幕?UMAR 的解决方案有效,但前提是在调用 setContentView() 之后设置了窗口属性。
回答by Pranav Mittal
Try this. It works for me.
尝试这个。这个对我有用。
dialog = new Dialog(Activity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.feedback_popup);
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
lp.gravity = Gravity.CENTER;
dialog.getWindow().setAttributes(lp);
回答by Miguel Rivero
Since API 8 FILL_PARENT is deprecated. Use MATCH_PARENT instead.
由于 API 8 FILL_PARENT 已弃用。请改用 MATCH_PARENT。
params.height = LayoutParams.MATCH_PARENT;