2012-01-03 40 views
0

我有LinearLayout與textView。如何更改從選擇器的textView顏色

我已經設置從選擇器的LinearLayout背景

android:background="@drawable/tab_bg_selector" 

因此按佈局時,它改變背景顏色

我的問題是,如何按佈局

時更改文本顏色也

我需要類似onStateChangeListner,所以當用戶按佈局它也改變文字顏色

謝謝,

託默

+0

你想爲你的textView使用選擇器嗎? – Praveenkumar 2012-01-03 12:35:24

+0

是的,我希望能夠改變文本的顏色,當狀態改變在選擇器 – 2012-01-03 12:41:12

回答

1

就與此代碼嘗試 -

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
} 
public void clickme(View view) 
{ 
    TextView tv = (TextView)findViewById(R.id.textView1); 
    tv.setTextColor(Color.RED);    
} 

main.xml中

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


<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:text="Large Text" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 
</LinearLayout> 

與您選擇嘗試。

+0

我試試這個,還是這個功能永遠不會叫 – 2012-01-03 12:55:41

+0

什麼?不叫?只是調試? – Praveenkumar 2012-01-03 12:56:15

+0

您是否完全複製我的代碼? – Praveenkumar 2012-01-03 13:00:04

3

把這個在的onClick佈局

textview.setTextColor(顏色);

+0

這不是幫助我,因爲我有我想要的所有狀態的選擇器,我只需要改變額外的textView顏色 – 2012-01-03 12:29:27

1

可以設置顏色使用XML標籤以兩種方式 到TextView的 「機器人:文本顏色=」 colorvalue」 或 組顏色由方法 「的TextView object.setTextColor(colorname)」,在Java代碼

4

重複的How to make shape's child TextView white when state_pressed="true"這是非常正確的答案duplicateParentState =「真正的」父ListView中選擇狀態轉移到TextView的孩子我下面的工作:。

<TextView 
    android:duplicateParentState="true" 
    android:textColor="@drawable/text_selector" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" /> 

和text_selector:

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

有一個解決方案 先用文本顏色製作不同顏色背景的兩個圖像 第二設定選擇爲你在上面像

android:background="@drawable/tab_bg_selector"

其definatly作品申請

相關問題