微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

原文地址:https://www.jb51.cc/android/315261.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐