【问题标题】:How to specify correct dialog size in XML layout file for Android dialog?如何在 Android 对话框的 XML 布局文件中指定正确的对话框大小?
【发布时间】:2016-09-27 14:26:39
【问题描述】:

我使用 XML 布局文件创建了一个 android 对话框,如下所示:

  final Dialog dialog = new Dialog(this);
        dialog.setContentView(R.layout.enabledialog);
        dialog.setTitle("Define message keys and setup");

        Switch locationSwitch = (Switch) dialog.findViewById(R.id.locationSwitch);
        Switch blareSwitch = (Switch) dialog.findViewById(R.id.blareSwitch);

        final EditText locationEdit = (EditText) dialog.findViewById(R.id.locationEdit);
        final EditText blareEdit = (EditText) dialog.findViewById(R.id.blareEdit);

        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.copyFrom(dialog.getWindow().getAttributes());
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;

        dialog.show();

        dialog.getWindow().setAttributes(lp);

但是,我的 XML 文件的内容只占屏幕的一半,所以当我显示对话框时,我显示了额外的空间,这看起来不太好:

这里是对话框外观的草图 (R.layout.enabledialog):

如何修剪这个多余的空间?在 android studio 中,布局编辑器假设 XML 文件占据整个屏幕,但实际上,我只想要一个小弹出窗口。

谢谢,

鲁基尔


编辑:这是我的布局资源文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#1dd1e9">


    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/locationSwitch"
        android:layout_marginTop="46dp"
        android:checked="false"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Blah"
        android:id="@+id/textView"
        android:textSize="30sp"
        android:layout_alignTop="@+id/locationSwitch"
        android:layout_centerHorizontal="true"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Blah Blah Blah"
        android:id="@+id/textView2"
        android:gravity="center"
        android:textSize="20sp"
        android:layout_below="@+id/locationSwitch"
        android:layout_centerHorizontal="true"
        />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/locationEdit"
        android:layout_below="@+id/textView2"
        android:layout_alignLeft="@+id/textView"
        android:layout_alignStart="@+id/textView"
        android:layout_alignRight="@+id/textView"
        android:layout_alignEnd="@+id/textView"
        android:singleLine = "true"
        android:imeOptions="actionDone" />

    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/blareSwitch"
        android:layout_marginTop="40dp"
        android:checked="false"
        android:layout_below="@+id/locationEdit"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Blah"
        android:id="@+id/textView3"
        android:textSize="30sp"
        android:layout_alignBottom="@+id/blareSwitch"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Blah Blah Blah"
        android:id="@+id/textView4"
        android:textSize="20sp"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/blareEdit"
        android:layout_below="@+id/textView4"
        android:layout_alignLeft="@+id/locationEdit"
        android:layout_alignStart="@+id/locationEdit"
        android:layout_alignRight="@+id/locationEdit"
        android:layout_alignEnd="@+id/locationEdit"
        android:singleLine = "true"
        android:imeOptions="actionDone" />
</RelativeLayout>

【问题讨论】:

  • 请发布布局资源文件。您可能还希望在 Java 代码中将 MATCH_PARENT 更改为 WRAP_CONTENT
  • @CommonsWare 已编辑 :)
  • @CommonsWare 我实际上在MATCH_PARENT 有它,因为当它是WRAP_CONTENT 时,内容重叠并且不合适。

标签: java android xml string performance


【解决方案1】:

尝试将根RelativeLayout 中的fill_parent 更改为wrap_content,以及将Java 代码中的MATCH_PARENT 更改为WRAP_CONTENT(或删除整个setAttributes() 位)。

我实际上在 MATCH_PARENT 有它,因为当它是 WRAP_CONTENT 时,内容重叠并且不合适

那么这是您需要解决的另一个问题。首先让对话框大小大致符合您的要求。然后,使用一些检查工具(AS 2.2 中的新工具、Hierarchy View、uiautomatorviewer 等)开始找出如何修复布局以避免重叠。

【讨论】:

  • 好的,我已将所有MATCH_PARENT'S 更改为WRAP_CONTENT',并且重叠仍在继续。我如何获得/使用您谈到的那些检查工具?谢谢
  • @RuchirBaronia:uiautomatorviewer 是 SDK 中的命令行工具。层次结构视图位于 Android 设备监视器中(可从 Android Studio 主菜单中的工具 > Android 中获得)。两者都包含在文档中,至少是轻微的。基本上,它们让您查看从设备加载的 UI 的实际内容,因此您可以在实际应用规则时可视化这些规则。
【解决方案2】:

尝试将布局参数高度更改为:

lp.height = WindowManager.LayoutParams.WRAP_CONTENT;

【讨论】:

    【解决方案3】:

    我通常以编程方式设置LayoutParams。我认为您可以在onResume 中使用以下代码尝试DialogFragment

    /**
     * Setup position of dialog 
     */
    protected void setupDialogPosition() {
        // setup the dialog
        WindowManager.LayoutParams p = getDialog().getWindow().getAttributes();
        p.width = ViewGroup.LayoutParams.MATCH_PARENT;
        p.y = 0;
        // change height of dailog
        if (mHeight == ViewGroup.LayoutParams.MATCH_PARENT) {
            p.height = mScreenHeight - p.y;
            if (isStatusBarVisible()) {
                p.height -= getStatusBarHeight();
            }
            } else {
            p.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        }
        getDialog().getWindow().setGravity(mGravity);
        getDialog().getWindow().setAttributes(p);
    }
    

    【讨论】:

      【解决方案4】:

      将RelativeLayout更改为LinearLayout:

      <LinearLayout
          android:layout_height="match_parent"
          android:layout_width="wrap_content"
      </LinearLayout>
      

      【讨论】:

        【解决方案5】:

        在您的相对布局中指定 layout_height 和 layout_width ,不要将其设置为 fill_parent 。 示例:

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="250dp"
        android:layout_height="wrap_content">
        
        <TextView
            android:id="@+id/customdia_text"
            android:layout_marginTop="20dp"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_centerHorizontal="true"
            android:text="hello"
            />
        <Button
            android:id="@+id/custdia_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Ok"
            android:layout_marginBottom="30dp"
            android:layout_marginTop="20dp"
            android:layout_below="@+id/customdia_text"
            android:layout_centerHorizontal="true"/>
        
        </RelativeLayout>
        

        【讨论】:

          【解决方案6】:

          直接从资源值转换:

          int width = getResources().getDimensionPixelSize(R.dimen.popup_width);
          int height = getResources().getDimensionPixelSize(R.dimen.popup_height);        
          getDialog().getWindow().setLayout(width, height);
          

          现在match_parent 在您的对话框布局中:

          android:layout_width="match_parent"
          android:layout_height="match_parent"
          
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          

          希望这会对你有所帮助。

          【讨论】:

            【解决方案7】:

            如下创建自定义对话框

            public class MyDialog extends Dialog {
            
                @Override
                public void onAttachedToWindow() {
                    super.onAttachedToWindow();
                    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
                    lp.width = WindowManager.LayoutParams.MATCH_PARENT;
                    lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            
                    getWindow().setAttributes(lp);
                }
            
                public MyDialog(Context context) {
                    super(context);
                }
            
                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.dialog);
                }
            }
            

            查看覆盖方法 onAttachedToWindow(),这是你的技巧。这个方法在 onCreate() 方法之前被调用。这样您就可以在创建对话框之前覆盖 windows 属性(宽度和高度)。

            从您的活动中调用此对话框,如下所示

            new MyDialog(ActivityContext).show();
            

            在 dialog.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="wrap_content"
                android:gravity="center"
                android:orientation="vertical">
            
                <Switch
                    android:id="@+id/locationSwitch"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_alignParentTop="true"
                    android:layout_marginTop="46dp"
                    android:checked="false" />
            
                <TextView
                    android:id="@+id/textView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignTop="@+id/locationSwitch"
                    android:layout_centerHorizontal="true"
                    android:text="Blah"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:textSize="30sp" />
            
                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/locationSwitch"
                    android:layout_centerHorizontal="true"
                    android:gravity="center"
                    android:text="Blah Blah Blah"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textSize="20sp" />
            
                <EditText
                    android:id="@+id/locationEdit"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignEnd="@+id/textView"
                    android:layout_alignLeft="@+id/textView"
                    android:layout_alignRight="@+id/textView"
                    android:layout_alignStart="@+id/textView"
                    android:layout_below="@+id/textView2"
                    android:layout_marginBottom="50dp"
                    android:imeOptions="actionDone"
                    android:singleLine="true" />
            
            </LinearLayout>
            

            如果您需要任何额外的自定义,请编辑布局文件

            【讨论】:

              【解决方案8】:

              根据 xml 文件中的要求更改 Main RelativeLayout 大小。指定您需要的高度,如下所示

              <?xml version="1.0" encoding="utf-8"?>
              <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="fill_parent"
                  android:layout_height="100dp"
                  android:background="#1dd1e9">
              
              <Switch
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:id="@+id/locationSwitch"
                  android:layout_marginTop="46dp"
                  android:checked="false"
                  android:layout_alignParentTop="true"
                  android:layout_alignParentLeft="true"
                  android:layout_alignParentStart="true" />
              
              <TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:textAppearance="?android:attr/textAppearanceLarge"
                  android:text="Blah"
                  android:id="@+id/textView"
                  android:textSize="30sp"
                  android:layout_alignTop="@+id/locationSwitch"
                  android:layout_centerHorizontal="true"
                  />
              
              <TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:textAppearance="?android:attr/textAppearanceSmall"
                  android:text="Blah Blah Blah"
                  android:id="@+id/textView2"
                  android:gravity="center"
                  android:textSize="20sp"
                  android:layout_below="@+id/locationSwitch"
                  android:layout_centerHorizontal="true"
                  />
              
              <EditText
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:id="@+id/locationEdit"
                  android:layout_below="@+id/textView2"
                  android:layout_alignLeft="@+id/textView"
                  android:layout_alignStart="@+id/textView"
                  android:layout_alignRight="@+id/textView"
                  android:layout_alignEnd="@+id/textView"
                  android:singleLine = "true"
                  android:imeOptions="actionDone" />
              
              <Switch
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:id="@+id/blareSwitch"
                  android:layout_marginTop="40dp"
                  android:checked="false"
                  android:layout_below="@+id/locationEdit"
                  android:layout_alignParentLeft="true"
                  android:layout_alignParentStart="true" />
              
              <TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:textAppearance="?android:attr/textAppearanceLarge"
                  android:text="Blah"
                  android:id="@+id/textView3"
                  android:textSize="30sp"
                  android:layout_alignBottom="@+id/blareSwitch"
                  android:layout_centerHorizontal="true" />
              
              <TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:textAppearance="?android:attr/textAppearanceSmall"
                  android:text="Blah Blah Blah"
                  android:id="@+id/textView4"
                  android:textSize="20sp"
                  android:layout_centerVertical="true"
                  android:layout_alignParentLeft="true"
                  android:layout_alignParentStart="true" />
              
              <EditText
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:id="@+id/blareEdit"
                  android:layout_below="@+id/textView4"
                  android:layout_alignLeft="@+id/locationEdit"
                  android:layout_alignStart="@+id/locationEdit"
                  android:layout_alignRight="@+id/locationEdit"
                  android:layout_alignEnd="@+id/locationEdit"
                  android:singleLine = "true"
                  android:imeOptions="actionDone" />
              </RelativeLayout>
              

              根据要求更改高度和宽度,而不需要 setAttributes(lp);在 Java 中。 如果您正在设置,请确保您考虑 xml RelativeLayout 高度和宽度

              【讨论】:

              • 我怎么知道哪个高度?
              • 请检查父级RelativeLaout中的代码,我已将高度设为100dp,使用它并根据要求进行更改。
              • 更改父布局高度检查代码并做跟踪和错误..
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-09-25
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多