2009-08-02 110 views
156

我想要一個簡單的TextView表現在ListView這樣做的方式。這裏的XML:Android選擇器和文字顏色

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="wrap_content" android:layout_width="fill_parent" 
    android:gravity="center" android:focusable="true" 
    android:minHeight="?android:attr/listPreferredItemHeight" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:background="@android:drawable/list_selector_background" /> 

一切正常,除了文本顏色(果然)不集中的狀態發生改變。我如何將其更改爲textAppearanceLargeInverse

+0

如果有人正在尋找xml文件,它在這裏: http://developer.android.com/resources/samples/Home/res/color/bright_text_dark_focused.html – swinefeaster 2010-08-12 22:29:53

回答

69

選擇器也是這裏的答案。

搜索來源bright_text_dark_focused.xml,添加到您的項目RES /彩色目錄下,然後從TextView的參考爲

android:textColor="@color/bright_text_dark_focused" 
+4

我認爲這已更改爲「primary_text_dark_focused.xml」。 – greg7gkb 2010-12-21 19:52:26

+10

這只是讓我失望。我一直在用聽衆處理事物以改變文字顏色。下巴在地板上。輝煌! – 2011-04-07 01:32:26

+0

謝謝!正是我在找的! – Zzokk 2012-02-09 14:04:40

0

你試過setOnFocusChangeListener?在處理程序中,您可以更改文本外觀。

例如:

TextView text = (TextView)findViewById(R.id.text); 
text.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
    public void onFocusChange(View v, boolean hasFocus) { 
     if (hasFocus) { 
      ((TextView)v).setXXXX(); 
     } else { 
      ((TextView)v).setXXXX(); 
     } 
    } 
}); 

然後,您可以申請你想,當它集中或沒有任何變化。您還可以使用ViewTreeObserver來偵聽全局焦點更改。

例如:

View all = findViewById(R.id.id_of_top_level_view_on_layout); 
ViewTreeObserver vto = all.getViewTreeObserver(); 
vto.addOnGlobalFocusChangeListener(new ViewTreeObserver.OnGlobalFocusChangeListener() { 
    public void onGlobalFocusChanged(
     View oldFocus, View newFocus) { 
     // xxxx 
    } 
}); 

我希望這可以幫助或者給你的想法。

+1

我已經這樣做了最初但文本由於某種原因,顏色沒有改變。 – yanchenko 2009-08-14 20:59:27

351

我做一些測試,直到一個工作,所以: RES /color/button_dark_text.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" 
      android:color="#000000" /> <!-- pressed --> 
    <item android:state_focused="true" 
      android:color="#000000" /> <!-- focused --> 
    <item android:color="#FFFFFF" /> <!-- default --> 
</selector> 

RES /佈局/ view.xml用

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="EXIT" 
     android:textColor="@color/button_dark_text" /> 
</LinearLayout> 
26

這裏是我的執行,這正好表現爲項目列表(至少在2.3)

RES /佈局/ list_video_footer.xml

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <TextView 
     android:id="@+id/list_video_footer" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="@android:drawable/list_selector_background" 
     android:clickable="true" 
     android:gravity="center" 
     android:minHeight="98px" 
     android:text="@string/more" 
     android:textColor="@color/bright_text_dark_focused" 
     android:textSize="18dp" 
     android:textStyle="bold" /> 

</FrameLayout> 

RES /顏色/ bright_text_dark_focused.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:state_selected="true" android:color="#444"/> 
    <item android:state_focused="true" android:color="#444"/> 
    <item android:state_pressed="true" android:color="#444"/> 
    <item android:color="#ccc"/> 

</selector> 
3

這裏是選擇器的例子。如果你使用eclipse,當你點擊ctrl和space時,它不會提示:/你必須輸入它。

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:drawable="@drawable/btn_default_pressed" android:state_pressed="true" /> 
<item android:drawable="@drawable/btn_default_selected" 
    android:state_focused="true" 
    android:state_enabled="true" 
    android:state_window_focused="true" /> 
<item android:drawable="@drawable/btn_default_normal" /> 

你可以看一下參考;

http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

2

我一直使用上述解決方案,無需搜索後,這更。 ;-)

但是,今天我遇到了一些東西,並想到分享它。:)

此功能確實可從API 1中獲得,並且被稱爲ColorStateList,我們可以在其中爲各種Widgets狀態(我們已知)提供顏色。使用TextViews在標籤此選擇定義爲我工作

中還非常有據可查,here.

1

如果(試過克勞斯Balduino的,但事實並非如此):

<?xml version="1.0" encoding="utf-8"?>  
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <!-- Active tab --> 
    <item 
    android:state_selected="true" 
    android:state_focused="false" 
    android:state_pressed="false" 
    android:color="#000000" /> 

    <!-- Inactive tab --> 
    <item 
    android:state_selected="false" 
    android:state_focused="false" 
    android:state_pressed="false" 
    android:color="#FFFFFF" /> 

</selector> 
17

爲了使其工作在列表視圖中選擇使用下面的代碼:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" android:color="#fff"/> 
    <item android:state_activated="true" android:color="#fff"/> 
    <item android:color="#000" /> 
</selector> 

顯然,關鍵是state_activated="true"狀態。