【问题标题】:How to set MaxWidth of an Android Dialog?如何设置 Android Dialog 的 MaxWidth?
【发布时间】:2011-05-26 18:14:44
【问题描述】:

我有一个自定义样式的对话框,它使用了我在 XML 文件中定义的布局。我希望这个对话框在纵向模式下填充屏幕的宽度,但在横向模式下只有大约 400dip。 MaxWidth 似乎是实现这一目标的完美方式,但我不知道如何将 MaxWidth 分配给对话框样式或 XML 布局。

【问题讨论】:

    标签: android layout dialog


    【解决方案1】:

    假设您使用名为 layout/dialog_core.xml 的下一个布局文件:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent" >
    
        //All your dialog views go here.... 
    
    </LinearLayout>
    

    然后您可以再创建两个文件:layout/dialog.xmllayout-land/dialog.xml

    layout/dialog.xml 文件不会限制宽度:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent" >
    
        <include layout="@layout.dialog_core.xml" />
    
    </LinearLayout>
    

    虽然你的 layout-land/dialog.xml 应该有宽度限制:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent"
        android:maxWidth="400dp" >
    
        <include layout="@layout.dialog_core.xml" />
    
    </LinearLayout>
    

    当然,您现在需要在代码中使用R.layout.dialog 布局。

    附:我仅将LinearLayout 用于示例目的。任何布局视图都可以。

    【讨论】:

    • 我试过了,但是 android:maxWidth 没有出现在 LinearLayout 的自动完成建议窗口中,所以我认为我不能使用它。无论如何,我尝试将其放入并且项目构建,但是该属性没有效果,并且对话框仍然伸展以填满屏幕。
    • @chefgon android:maxWidth 不是您想要的。你想要的是在纵向模式下android:layout_width="fill_parent"。按照 inazaruk 说的做,不要忘记将 dialog-land.xml 放在布局文件夹 res/layout-land 中。 Android 会根据屏幕方向自动加载正确的布局(dialog.xmldialog-land.xml)。
    • 据我所知,根据开发文档,dialog-land.xml 也应该命名为 dialog.xml,但正如 @Turbo 所说,放在 res/layout-land 中 - 我相信 @inazaruk 命名在他的解释过程中以不同的方式区分它们。
    【解决方案2】:

    你可以在这里找到一个实际的解决方案:setting a max width for my dialog

    答案是使用对话框主题的活动,但考虑到它是基于 Window 对象的,它应该太复杂而无法适应对话框。

    【讨论】:

      【解决方案3】:

      我通过在显示对话框后更改对话框的窗口布局来实现 BottomSheetDialog:

      private int maxWidthDp = 600;
      
      public void showDialog(Context context) {
          BottomSheetDialog dialog = new BottomSheetDialog(context);
          // set up the dialog...
          dialog.setOnShowListener(new DialogInterface.OnShowListener() {
              @Override
              public void onShow(DialogInterface dialogInterface) {
                  Configuration configuration = context.getResources().getConfiguration();
                  if (configuration.screenWidthDp > maxWidthDp) {
                      // ensure maximum dialog size
                      dialog.getWindow().setLayout(dpToPixels(maxWidthDp), -1);
                  }
              }
          });
          dialog.show();
      }
      

      dpToPixels 是一个辅助函数,用于将 dp 值转换为像素值,例如喜欢这里https://stackoverflow.com/a/8399445/1396068

      【讨论】:

        【解决方案4】:

        您是否尝试在自定义 xml 中设置 android:minWidthandroid:minHeight

        【讨论】:

        • 不,但我不明白 minWidth 和 minHeight 如何帮助我设置最大宽度。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-23
        • 1970-01-01
        • 2018-08-29
        • 2022-01-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多