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

8
我在应用程序中有一个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" />

我曾认为在textAppearance属性中使用textAppearanceSmallInverse是设置文本参数的正确方式,而且它应该能够在所有设备上正常工作,但似乎我错了。那么,我应该怎么做才能使AlertDialog在所有平台上正确显示列表项呢?提前感谢您的帮助。

1
这个问题很旧了,但也许还有其他人会从中受益。解决方案是利用Android内置的资源选择来实现你所需要的功能。请参考这篇教程获取指导。 - howettl
我在我的项目中使用了完全相同的方法,但希望存在更好的解决方案。 - Michael
我的上面的评论是从我的答案中提取的。 - howettl
你可能不小心发布了评论而不是答案? - Michael
我认为版主可能会删除你的回答,因为你只是发布了一个博客链接。如果你编辑回答并添加该文章的摘要,我会接受你的回答,并且它将更有助于其他人。 - Michael
显示剩余2条评论
4个回答

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>

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

请参考上面链接的教程,以获取更详细的解释。

1

弹出对话框的代码应该类似于这样:

// Sets dialog for popup dialog list
AlertDialog dialog;
String[] items = {"exampleItem"};
ListAdapter itemlist = new ArrayAdapter(this, android.R.layout.simple_list_item_1, items);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Title");
builder.setAdapter(itemlist, new DialogInterface.OnClickListener()
{
    public void onClick(DialogInterface dialog, int item)
    {
    }
});
dialog = builder.create();
dialog.getListView().setBackgroundColor(Color.WHITE);

在这里,您正在获取列表视图并将背景颜色设置为白色。如果您想更改每个文本视图的文本颜色,则需要在文本视图的布局中定义它们的颜色,在这种情况下为黑色:

android:textColor="#000000"

谢谢您的回复,但我不想改变列表的背景。我希望它在特定平台上看起来像应该看起来的那样。我只想设置适当的文本样式,使其在每个平台上都可读。 - Michael

1
被接受的回答似乎有点过于复杂了。我只是通过调用以下代码来强制反向背景色:
dialogBuilder.setInverseBackgroundForced(true);

解决问题很好。

就我所记得的,这个标志在Android 2.x和Android 3.x上都不能解决问题。 - Michael
1
setInverseBackgroundForced现在已经过时。 - Gabriel Ferreira

0
这可能是因为您没有指定主题,然后它会回退到默认主题。在2.x中,应该是Theme.Black,在3.x中是Theme.Holo(或Theme.Light,不确定)。然后textAppearanceSmallInverse在每个主题中解析为不同的样式。

我在2.x中使用'Theme',在3.x中使用'Theme.Holo'。但问题是'AlertDialog'在不同平台上使用相反的样式。但两个平台都使用相同的标题文本样式。这是奇怪的行为。 - Michael

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