在我的应用中,我有一个标签小部件,我想将其选定的标签更改为不同的背景颜色。我已经使用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>
答案 0 :(得分:0)
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
}