2011-07-14 17 views
7

我的應用程序中有一個AlertDialog。它包含一個自定義視圖列表,其中包含TextView小部件。一切工作正常在Android 2.x. AlertDialog是使用白名單和黑色文本創建的。但是當我在Android 3.x設備上運行我的應用程序時,所有TextView都是黑色,列表的背景也是黑色。所以我不能看到文字,直到我點擊並按住其中一個項目。Android:如何正確設置AlertDialog中列表項的文本顏色

下面是從佈局文件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" /> 

我想到用textAppearanceSmallInversetextAppearance屬性設置文本參數的正確方法,它必須在所有設備上工作,但看來我是錯誤。那麼我應該怎麼做才能在所有平臺上正確顯示列表項?提前致謝。

+1

這個問題還算舊,但也許別人會從中受益。解決方案是利用Android的內置資源選擇來實現您正在尋找的內容。有關說明,請參閱[本](http://kmansoft.com/2011/10/15/alertdialog-custom-listview-items-and-honeycomb/)教程。 – howettl

+0

我在我的項目中使用了完全相同的方法,但希望有更好的解決方案。 – Michael

+0

爲什麼我的答案被刪除? – howettl

回答

2

的解決方案是利用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" /> 

見上面掛了一個冗長的解釋教程。

0

這可能是因爲您沒有指定主題,然後又回到默認主題。在2.x這應該是Theme.Black和3.x Theme.Holo(或Theme.Light,不知道這一點)。然後textAppearanceSmallInverse在每個主題中解決不同的風格。

+0

我在2.x中使用「主題」,在3.x中使用「Theme.Holo」。但問題是'AlertDialog'在不同的平臺上使用相反的樣式。但是這兩個平臺對標題文本使用相同的樣式。這是奇怪的行爲。 – Michael

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

在這裏,你所得到的ListView和背景顏色設置爲白色。如果你想改變文本的顏色爲每個textviews那麼你需要定義TextView的佈局他們的膚色,在這種情況下,黑色:

android:textColor="#000000" 
+0

感謝您的回覆,但我不想更改列表的背景。我希望它看起來應該看起來在特定的平臺上。我想要的只是設置適當的文本樣式,以便在每個平臺上都可讀。 – Michael

1

接受的答案似乎有點矯枉過正。我只是通過調用強制反轉背景:

dialogBuilder.setInverseBackgroundForced(true); 

解決了這個問題。

+0

據我記得這個標誌並不能解決Android 2.x和Android 3.x上的問題。 – Michael

+1

setInverseBackgroundForced現已棄用 –

相關問題