Android:如何正确设置AlertDialog中列表项的文本颜色

Mic*_*ael 8 android themes android-alertdialog

我的AlertDialog应用程序中有一个.它包含一个包含TextView小部件的自定义视图列表.在Android 2.x上一切正常.在AlertDialog与白名单和它的黑白文本创建.但是当我在Android 3.x设备上运行我的应用时,所有TextView的都是黑色的,列表的背景也是黑色的.所以在我点击并按住其中一个项目之前,我看不到文字.

这是TextView布局文件中的定义:

<TextView
    android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:ellipsize="marquee"
    android:textAppearance="?android:attr/textAppearanceSmallInverse" />
Run Code Online (Sandbox Code Playgroud)

我认为,使用textAppearanceSmallInversetextAppearance属性设置文本参数的正确方法,它必须在所有设备上工作,但看来我错了.那么我应该怎么做才能AlertDialog在所有平台上正确显示列表项?提前致谢.

how*_*ttl 3

解决方案是利用 Android 内置的资源选择系统。您应该指定两种不同的样式,并根据 API 版本将它们放置在适当的文件夹中。请注意,以下示例不是我的,我是从教程中摘取的。

res/values-v4/styles.xml:

<resources>

<!-- Text for listboxes, inverted for Andorid prior to 3.0 -->

<style name="MyListTextAppearanceSmall">
    <item name="android:textAppearance">?android:attr/textAppearanceSmallInverse</item>
</style>

<style name="MyListTextAppearanceDefault">
    <item name="android:textAppearance">?android:attr/textAppearanceInverse</item>
</style>

<style name="MyListTextAppearanceMedium">
    <item name="android:textAppearance">?android:attr/textAppearanceMediumInverse</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)

res/values-v11/styles.xml:

<resources>
    <!-- Text for listboxes, non-inverted starting with Android 3.0 -->

    <style name="MyListTextAppearanceSmall">
        <item name="android:textAppearance">?android:attr/textAppearanceSmall</item>
    </style>

    <style name="MyListTextAppearanceDefault">
        <item name="android:textAppearance">?android:attr/textAppearance</item>
    </style>

    <style name="MyListTextAppearanceMedium">
        <item name="android:textAppearance">?android:attr/textAppearanceMedium</item>
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

然后,在您的 中TextView指定样式,如下所示:

<TextView
    android:style="@style/MyListTextAppearanceSmall"
    android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:ellipsize="marquee" />
Run Code Online (Sandbox Code Playgroud)

请参阅上面链接的教程以获得更详细的解释。