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

标签 android themes android-alertdialog

我的应用程序中有一个 AlertDialog。它包含一个自定义 View 列表,其中包含 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" />

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

最佳答案

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

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>

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>

然后,在您的 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:如何正确设置 AlertDialog 中列表项的文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6698350/

相关文章:

android - 如何从 Flutter Plugin 访问 Android 资源字符串

android - 如何在 Xamarin.Android 中使用 "values-v11"文件夹

rubygems - Jekyll:安装基于 gem 的主题

android - 我不想要一个以上的 Activity 实例

android - 将 FILL_PARENT 宽度设置为位于 TableRow 内部的 TextView

Android轻松切换应用程序的配色方案

android - 我应该在哪里添加我的 alertdialog 以及如何获取 json 中的值?

android - 在 Android 中,如何像在 Google map 中那样显示带有方角的警告对话框?

android - 在单选警报对话框中启用/禁用肯定按钮

android - 当我选择其他项目时,BottomNavigationView 中的第一项保持突出显示