[英]How can I set different color for my tab label independent of background color?
如何為標簽標簽設置單獨的顏色? 如果我改變背景顏色整個顏色的變化。
我的代碼
<TabHost android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/tabhost"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@android:id/tabs"
android:layout_alignParentBottom="true"/>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/tabcontent">
</FrameLayout>
</RelativeLayout>
</TabHost>
你可以通過這樣做來做這件事
String TAG_AddData="TAB LABEL";
/*add Tab in Tabgroup*/
TabHost host = getTabHost();
host.addTab(host
.newTabSpec(TAG_AddData)
.setIndicator(TAG_AddData,
getResources().getDrawable(R.drawable.tab_add))
.setContent(
new Intent(this, AddData_ActivityGroups.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
/ *現在你可以遍歷所有選項卡以獲得其標簽屬性* /
for (int i = 0; i < host.getTabWidget().getChildCount(); i++) {
TextView tv = (TextView) host.getTabWidget().getChildAt(i)
.findViewById(android.R.id.title);
tv.setTextColor(Color.parseColor("#ffffff"));
}
TextView tv = (TextView) host.getCurrentTabView().findViewById(
android.R.id.title); // for Selected Tab
tv.setTextColor(Color.parseColor("#000000"));
同樣你可以在tabGroup的onTabChanged方法中應用
res/drawable/tabselector.xml
<selector
android:id="@+id/myselector"
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item
android:state_focused="false"
android:state_selected="false"
android:state_pressed="false"
android:drawable="@drawable/darklogo" />
<item
android:state_focused="false"
android:state_selected="true"
android:state_pressed="false"
android:drawable="@drawable/lightlogo" />
<!-- Focused states -->
<item
android:state_focused="true"
android:state_selected="false"
android:state_pressed="false"
android:drawable="@drawable/lightlogo" />
<item
android:state_focused="true"
android:state_selected="true"
android:state_pressed="false"
android:drawable="@drawable/lightlogo" />
<!-- Pressed -->
<item
android:state_pressed="true"
android:drawable="@drawable/lightlogo" />
</selector>
您在此處包含的XML是一種定義drawable的方法,可以嵌入case語句。 它根據分配給它的視圖的狀態呈現不同的drawable。 作為drawable,您應該將其保存為項目的res/drawable
文件夾中的xml文件(例如tabselector.xml
)。
要將它用於Tabhost,您需要像平常一樣構造TabActivity(如本教程示例所示)。
然后,當您將每個選項卡添加到主機時,將tabselector
drawable指定為指示符,如下面的“TAB 1”所示。
Drawable mySelector = getResources().getDrawable(R.drawable.tabselector);
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 1", mySelector).setContent(R.id.textview1));
mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB 2").setContent(R.id.textview2));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.