Android对话框宽度

Sol*_*lid 50 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_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>
Run Code Online (Sandbox Code Playgroud)

由于某种原因,对话框只有足够宽的文本输入字段大约11个字符宽.如何使对话框宽度填满屏幕?

UMA*_*MAR 86

我有同样的问题.

我使用下面的代码来创建对话框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);
    }
}
Run Code Online (Sandbox Code Playgroud)

布局

<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>
Run Code Online (Sandbox Code Playgroud)

  • 现在不推荐使用`fill_parent`来支持`match_parent`. (2认同)

hru*_*872 56

我正在使用:

getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
Run Code Online (Sandbox Code Playgroud)


roy*_*hew 16

在最顶部的布局中设置最小宽度.

android:minWidth="300dp"
Run Code Online (Sandbox Code Playgroud)

例如:

<?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>
Run Code Online (Sandbox Code Playgroud)

  • 对我来说更好的解决方案,因为我希望将定义完全保留在XML中,而不必在代码中修复它.谢谢你指出这个属性! (2认同)

Ale*_*lav 13

正如Matthias指出的那样我如何获得一个Dialog风格的活动窗口来填充屏幕?UMAR的解决方案有效,但只有在调用setContentView()后才设置窗口属性.


小智 7

试试这个.这个对我有用.

    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);
Run Code Online (Sandbox Code Playgroud)