Android AlertDialog产生黑色背景和黑色文字。

5

我在Android API 10的自定义视图对话框中遇到了问题。

我使用AlertDialog.Builder构建对话框。我使用构建器上的setView命令来包含自定义视图面板。

这在我测试的大多数API上都有效。风格从设备到设备有所不同,但这就是我想要的,即样式与设备默认值匹配。

我的问题是,在API 10上,任何出现在我的自定义视图中的文本都显示为黑色背景上的黑色。

任何使用AlertDialog.Builder.setMessage()插入的文本都会正确显示。

对话框构建器使用了什么神奇的属性/样式来确定文本外观?

我的应用程序主题是Theme.AppCompat.Light

下面是我的onCreateDialog方法:

    public Dialog onCreateDialog(Bundle savedInstanceState) {
    LayoutInflater inflater = getActivity().getLayoutInflater();
    final View view = inflater.inflate(R.layout.fragment_status_dialog, null);
    mStatusTextView = (TextView) view.findViewById(R.id.text_status);
    mConnectedDeviceTextView = (TextView) view.findViewById(R.id.text_connected_device);

    MainService.ServiceState state = null;
    if (getArguments().containsKey(SERVICE_STATUS_ARG_KEY)) {
        state = (MainService.ServiceState) getArguments().getSerializable(SERVICE_STATUS_ARG_KEY);
    }
    setState(state);

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setView(view);
    builder.setMessage("This will show up just fine.");
    builder.setTitle(getString(R.string.status_title));
    builder.setNegativeButton(R.string.dialog_back_button_text, null);
    builder.setNeutralButton(R.string.dialog_connect_to_text, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            mListener.onDialogConnectTo();
        }
    });
    // Create the AlertDialog object and return it
    return builder.create();
}

这是我的fragment_status_dialog布局。
    <?xml version="1.0" encoding="utf-8"?>

    <RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="18dp">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="@string/status_starting"
    android:id="@+id/text_status"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="@string/status_connected_to_unknown"
    android:paddingStart="4dp"
    android:paddingLeft="4dp"
    android:paddingRight="4dp"
    android:id="@+id/text_connected_device"
    android:layout_toRightOf="@+id/text_status"
    android:layout_toEndOf="@+id/text_status"/>
    </RelativeLayout>

注意,我尝试了https://stackoverflow.com/a/24505312/2350083,但它并没有解决问题。 ex

1
尝试调用 AlertDialog#setInverseBackgroundForced(true) - Alex Lockwood
Alex,这个解决方案对我来说似乎有效!谢谢你!到目前为止,我已经在使用API 10、13、15、19的设备上进行了测试。 - Jon
很高兴听到这个消息!我已经将它添加为一个答案。 - Alex Lockwood
2个回答

2
尝试调用 AlertDialog#setInverseBackgroundForced(true)

0

那么直接设置文字的颜色呢?例如:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@android:color/white"
    android:text="@string/status_starting"
    android:id="@+id/text_status"/>

TextView使用设备(或应用程序)的默认文本颜色。如果您特别设置了TextView的颜色,则会在不考虑API的设备上被覆盖。

1
不同的API具有不同的样式。例如,我的Dell Streak 7显示所有对话框都是白色背景。 - Jon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接