【问题标题】:Custom layout for Alert Dialog not working on older API versions警报对话框的自定义布局不适用于旧 API 版本
【发布时间】:2015-08-07 10:21:26
【问题描述】:

我正在尝试创建自定义警报对话框。让它在较新版本的 Android 上工作,但是当我在 2 台安装了 2.3.7 的设备上测试它时,我的自定义布局不可见(见下图)。

这是我用来膨胀并将我的自定义视图附加到 alertDialog 的代码:

LayoutInflater inflater = activity.getLayoutInflater();
View customView = inflater.inflate(R.layout.adjust_layout_dialog, null);

mDialog = new AlertDialog.Builder(activity)
            .setView(customView)
            .setTitle(mContext.getString(R.string.adjust_layout_dialog_title))
            .setPositiveButton(mContext.getString(R.string.adjust), null) 
            .setNegativeButton(mContext.getString(R.string.cancel), null)
            .setNeutralButton(mContext.getString(R.string.reset_to_default), null)
            .create();

mDialog.show();

如何让它在 2.3.7 上运行?

2.3.7 上的布局:

4.x 上的布局:

5.x 上的布局:

【问题讨论】:

    标签: android android-layout dialog android-custom-view android-alertdialog


    【解决方案1】:

    你要申请吗

    android:theme="@android:style/Theme.Holo.Light.Dialog"
    

    如果您在 api 10 或更低版本中使用此主题,则它不起作用 你可以改用:

    android:theme="@android:style/Theme.Dialog"
    

    【讨论】:

      【解决方案2】:

      在我的 onCreate() 中将主题设置为 Theme_Dialog,例如:

         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
                  setTheme(android.R.style.Theme_Dialog);
              } 
      

      确实让我的 dialogText 可见,但主要是我试图在深色背景上显示深色文本。当我更改自定义布局的背景色时

      (layout.setBackgroundColor(Color.WHITE); f.e.)

      我实际上发现了正在绘制的文本加按钮。

      编辑:

        .setInverseBackgroundForced(true)
      

      在 AlertDialog.Builder 上实际上提供了更好的结果,为自定义视图的整个背景区域提供浅色,同时不更改 SDK 4.+ 和 5.+ 布局上的背景颜色。

      【讨论】:

        猜你喜欢
        • 2014-11-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多