【问题标题】:Android dialog widthAndroid 对话框宽度
【发布时间】:2011-02-07 18:09:03
【问题描述】:

我似乎无法控制对话框宽度。我有一个像这样的简单布局`

<?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 个字符宽的文本输入字段。如何让对话框的宽度填满屏幕?

【问题讨论】:

标签: android dialog width


【解决方案1】:

我遇到了同样的问题。

我使用以下代码制作了对话框 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);
    }
}

布局

<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>

【讨论】:

  • 由于某种原因,我为宽度/高度设置的 xml 布局参数在我的对话框中不起作用,但这很好用! +1
  • 这对我有用,但前提是我在 调用 show() 之后重置参数。
  • fill_parent 现在已弃用,取而代之的是 match_parent
【解决方案2】:

试试这个。它对我有用。

    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);

【讨论】:

  • 如果您在 onActivityCreated 中执行此操作,如果您正在使用 DialogFragment 将起作用。
    getDialog().getWindow().getAttributes().width = (int) getResources().getDimension(R. dimen.uid_alert_dialog_width);也应该工作
【解决方案3】:

我正在使用:

getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

【讨论】:

  • FILL_PARENT 现已弃用。
  • 应该改用 MATCH_PARENT
  • 所以它会像:dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
【解决方案4】:

由于 API 8 FILL_PARENT 已被弃用。请改用 MATCH_PARENT。

params.height = LayoutParams.MATCH_PARENT;

【讨论】:

  • 这是另一个问题的答案...弃用并不意味着禁用(还)
  • 我同意,deprecated 并不意味着禁用,而是意味着即使您可以继续使用它也可能不会这样做,因为可能会在下一个版本中删除。
  • 仍然不是这个问题的答案。由于 FILL_PARENT 仍然有效,因此您不能指望通过替换为首选同义词来解决此问题。
【解决方案5】:

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

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>

【讨论】:

  • 如果它在最外面的布局中不起作用,因为它不适合我,它可以通过将它设置在一个布局元素中来工作,比如标题栏。
  • 对我来说更好的解决方案,因为我想将定义完全保留在 XML 中,而不必在代码中修复它。感谢您指出这个属性!
  • 这对我来说适用于 Kotlin 和 ConstraintLayout
【解决方案6】:

正如 Matthias 指出的 How can I get a Dialog style activity window to fill the screen? UMAR 的解决方案有效,但前提是在调用 setContentView() 之后设置了窗口属性。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2014-07-13
  • 1970-01-01
  • 2018-02-25
  • 2011-06-23
  • 2018-04-21
  • 1970-01-01
  • 1970-01-01
  • 2020-03-01
相关资源
最近更新 更多