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" />

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

解决方法

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

RES /值-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>

RES /值-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>

然后,在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" />

请参阅上面链接的教程以获得更长的解释.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...