警报对话框的自定义布局不适用于较旧的API版本

时间:2015-08-07 10:21:27

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

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

这是我用来充气的代码&将我的自定义视图附加到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上的布局: Layout not visible on 2.3.7

4.x上的布局: Custom Layout on API 4.x

5.x上的布局: Custom Layout on API 5.x

2 个答案:

答案 0 :(得分:0)

你试图申请

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

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

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

答案 1 :(得分:0)

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

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

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

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

我实际上找到了正在绘制的文字和按钮。

编辑:

  .setInverseBackgroundForced(true)

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