2011-06-15 47 views
2

在我的seTabColor()我設置標題文本的顏色爲灰色。我想在按下時將其更改爲白色。我該怎麼做?如何按下時改變標籤的文字顏色(currentTab)?

public void setTabColor(TabHost tabHost) { 
     for(int i = 0; i<tabHost.getTabWidget().getChildCount(); i++) { 
//   tabHost.getTabWidget().getChildAt(i).setBackgroundResource(r[i]); 
      tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.BLACK); 
      TextView t = (TextView) getTabWidget().getChildAt(i).findViewById(android.R.id.title); 
      t.setTextSize(9 * getResources().getDisplayMetrics().density); 
//   tabHost.getTabWidget().getChildAt(i).getLayoutParams().height = 58; 
//   tabHost.getTabWidget().getChildAt(i).().height = 58; 
      TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title); 
      tv.setTextColor(Color.GRAY); 

}

我想要做的是這樣的:tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())...

但林不知道如何使用條件的改變文字顏色。

回答

1

首先,看看如何在XML中定義你的UI。請參閱State List Drawable Resource。您可以定義在按下視圖,突出顯示等時使用的圖像。定義之後,您可以像使用其他資源一樣使用XML文件。

例子:

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

This layout XML applies the state list drawable to a Button: 

<Button 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:background="@drawable/button" /> 
5

嘗試this answer,其中特別節目:保存在RES /繪製/ button.xml XML文件

<item name="android:textColor">@android:color/tab_indicator_text</item> 

您可以通過創建覆蓋該默認textColor您自己的顏色選擇器(在您的項目中創建res/color/目錄,並在其中創建一個新文件,稱爲tab_indicator_text.xml),然後更改上面的值以匹配您自己的顏色選擇器(@color/tab_indicator_text)。該tab_indicator_text.xml文件的內容將是一個選擇列表,像this answer提到的:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_selected="true" android:color="@color/white" /> 
    <item android:state_focused="true" android:color="@color/white" /> 
    <item android:state_pressed="true" android:color="@color/white" /> 
    <item android:color="#bfbfbf" /> 
</selector> 
相關問題