[英]How to create a customize android tab widget, that the selected tab will change its default color background?
在我的應用程序中,我有一個選項卡小部件,我想將其選定的選項卡更改為其他背景顏色。 我已經使用android xml布局將標簽小部件的初始背景設置為粉紅色,然后,如果我想從標簽小部件中選擇標簽,我想將所選標簽的背景更改為灰色。
這是我的標簽托管活動的代碼:
public class TabHostActivity extends TabActivity {
private TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_tabhost);
UserAccount userAccount = new UserAccount();
tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabTitle = (TextView) findViewById(R.id.tabTitle);
TabSpec tab1 = tabHost.newTabSpec("First Tab");
tab1.setContent(new Intent(this, HomeActivity.class));
tab1.setIndicator("",getResources().getDrawable(R.drawable.home_icon));
tabHost.addTab(tab1);
TabSpec tab2 = tabHost.newTabSpec("Second Tab");
tab2.setContent(new Intent(this, About.class));
tab2.setIndicator("",getResources().getDrawable(R.drawable.about_icon));
tabHost.addTab(tab2);
TabSpec tab3 = tabHost.newTabSpec("Third Tab");
tab3.setContent(new Intent(this, GridViewActivity.class));
tab3.setIndicator("",getResources().getDrawable(R.drawable.gallery_icon));
tabHost.addTab(tab3);
}
}
這是我的xml文件,我將選項卡小部件的背景設置為粉紅色:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
</FrameLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="#FCAFA6"
android:tabStripEnabled="false">
</TabWidget>
</LinearLayout>
</TabHost>
您可以更改Tabwidget顏色
public static void setTabColor(TabHost tabhost) {
for(int i=0;i<tabhost.getTabWidget().getChildCount();i++) {
tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#FF0000")); //unselected
}
tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.parseColor("#0000FF")); // selected
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.