2013-05-10 68 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

相关问题