2013-05-10 62 views
1

我能夠在StackOverflow上的問題幫助下實現自定義選項卡視圖。所以我有一個自定義選項卡視圖如下。根據自定義選項卡視圖的狀態設置文本顏色

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:id="@+id/TabLayout" 
android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:padding="5dip"> 

<ImageView android:id="@+id/TabImageView" android:layout_width="wrap_content" android:layout_height="wrap_content"/> 

<TextView android:id="@+id/TabTextView" android:text="Text" android:paddingTop="5dip"  android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
/> 

</LinearLayout> 

我它充氣如下

MainActivity.addTab(this, this.mTabHost,  this.mTabHost.newTabSpec("Tab1").setIndicator(prepareTabView("Smoker",  R.drawable.ic_smoker)), (tabInfo = new TabInfo("Tab1", MainFragment.class, args))); 

功能如下所示

private View prepareTabView(String text, int resId) { 
    View view = LayoutInflater.from(this).inflate(R.layout.tab, null); 
    ImageView iv = (ImageView) view.findViewById(R.id.TabImageView); 
    TextView tv = (TextView) view.findViewById(R.id.TabTextView); 
    iv.setImageResource(resId); 
    tv.setText(text); 
    return view; 
} 

我對繪製資源定義的樣式如下

<selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/ic_smoker_green" /> <item android:drawable="@drawable/ic_smoker_red" /> </selector>

我的問題是我如何根據選項卡的狀態來改變文本顏色?

回答

1

在主XML的TextView設爲

android:textColor="@drawable/text_col" 

,並繪製創建另一個文件說text_col.xml,我希望這會做

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

<item android:state_focused="true" android:state_pressed="false" android:color="#ff7BCFFC" /> 
<item android:state_focused="true" android:state_pressed="true" android:color="#ff7BCFFC" /> 
<item android:state_focused="false" android:state_pressed="true" android:color="#ff7BCFFC" /> 

<item android:state_selected="true" android:color="@drawable/box_col" ></item> 
<item android:color="#ffffffff" /> 

+0

歡迎您:) – bala 2013-05-10 10:21:53

相關問題