Android对话框宽度
我似乎无法控制对话框的宽度。我有这样一个简单的布局`Android对话框宽度,android,dialog,width,Android,Dialog,Width,我似乎无法控制对话框的宽度。我有这样一个简单的布局` <?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_heig
<?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>
由于某些原因,对话框的宽度仅足以容纳大约11个字符宽的文本输入字段。如何使对话框宽度填满屏幕?我也有同样的问题 我使用下面的代码使对话框填充成为父对象,它工作得很好
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);
}
}
布局
<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>
这里的内容
正如Matthias指出的那样,out UMAR的解决方案是有效的,但只有在调用setContentView()后设置窗口属性时才有效。我使用的是:
getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
在最顶部的布局处设置最小宽度
android:minWidth="300dp"
例如:
<?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>
因为API 8填充父项已被弃用。改为使用匹配父项
params.height = LayoutParams.MATCH_PARENT;
试试这个。它对我有用
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);
出于某种原因,我为宽度/高度设置的xml布局参数在我的对话框中不起作用,但这非常有效+1这对我有效,但前提是我在调用
show()
fill\u parent
后重置参数,现在反对使用match\u parent
。对我有效的唯一解决方案是:实际上,正如这里所述,[,它只在.show()之后有效(至少对我有效).这是另一个问题的答案…弃用并不意味着禁用(尚未)我同意,弃用并不意味着禁用,但这意味着即使您可以继续使用它,您也可能不会这样做,因为在下一个版本中可能会被删除。仍然不是这个问题的答案。因为FILL\u PARENT仍然有效,您不能期望通过替换为首选同义词来解决此问题。如果它在outerm中不起作用ost布局,因为它不适用于我,它通过在布局元素之一(如标题栏)中设置它来工作。对我来说,更好的解决方案是,我希望将定义完全保留在XML中,而不必在代码中对其进行修改。感谢您指出此属性!这对我适用于Kotlin和ConstraintLayoutFILL\u PARENT现在不推荐使用。应该使用M吗ATCH_PARENT instead如果您在使用DialogFragment创建的OnActivity中执行此操作,则可以使用。getDialog().getWindow().getAttributes().width=(int)getResources().getDimension(R.dimen.uid\u alert\u dialog\u width);也可以使用