Android: How to set text color for list items in AlertDialog properly

I have an AlertDialog in my application. It contains a list of custom views with TextView widgets inside. Everything works fine on Android 2.x. The AlertDialog is created with white list and black text in it. But when I run my app on Android 3.x devices all TextView s are black and list's background is black too. So I can't see the text until I tap and hold one of the items.

Here's a TextView 's definition from the layout file:

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

I thought that using textAppearanceSmallInverse for the textAppearance attribute is a proper way to set text parameters and it must work on all devices but seems I was wrong. So what should I do to make AlertDialog display list items properly on all platforms? Thanks in advance.


The solution is to leverage Android's built-in resource selection system. You should specify two different styles and place them in the proper folders based on the API version. Note that the following examples are not mine, I took them from this tutorial.

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>

Then, in your TextView , specify the style like so:

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

See the tutorial linked above for a lengthier explanation.


Your code for the popup dialog should look similar to this:

// 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);

Here, you are getting the listview and setting the background color to white. If you want to change the color of the text for each of the textviews then you need to define their color in the layout of the textview, in this case black:

android:textColor="#000000"

The accepted answer seems like a bit of overkill. I simply forced the inverse background by calling:

dialogBuilder.setInverseBackgroundForced(true);

solves the problem just fine.

链接地址: http://www.djcxy.com/p/53598.html

上一篇: 提交给Apple:XCode“submit”或Application Loader?

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