我想在Android的Tab中红色圆圈内的Header中添加一些文字。 要制作标签,我使用了以下代码。
MyActivity.Java
public class MainActivity extends TabActivity implements OnTabChangeListener {
TabHost tabHost;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost.TabSpec spec;
tabHost = getTabHost();
// Android tab
Intent intentAndroid = new Intent().setClass(this, Aclass.class);
spec = tabHost.newTabSpec("Android").setContent(intentAndroid)
.setIndicator("My Profile ");
tabHost.addTab(spec);
tabHost.setOnTabChangedListener(this);
// Welcome tab
Intent intentBus = new Intent().setClass(this, Bclass.class);
spec = tabHost.newTabSpec("Welcome").setIndicator("Card View")
.setContent(intentBus);
tabHost.addTab(spec);
tabHost.getTabWidget().getChildAt(1)
.setBackgroundResource(R.drawable.two);
tabHost.getTabWidget().setCurrentTab(0);
tabHost.getTabWidget().getChildAt(0)
.setBackgroundResource(R.drawable.one);
}
@Override
public void onTabChanged(String tabId) {
Log.i("tabs", "CurrentTab: " + tabHost.getCurrentTab());
if (tabHost.getCurrentTab() == 0) {
tabHost.getTabWidget().getChildAt(0)
.setBackgroundResource(R.drawable.one);
tabHost.getTabWidget().getChildAt(1)
.setBackgroundResource(R.drawable.two);
}
if (tabHost.getCurrentTab() == 1) {
tabHost.getTabWidget().getChildAt(0)
.setBackgroundResource(R.drawable.one_hover);
tabHost.getTabWidget().getChildAt(1)
.setBackgroundResource(R.drawable.two_hover);
}
}
}
XMl代码
activity_main.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:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:tabStripEnabled="false"
android:showDividers="none"
/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
</TabHost>
答案 0 :(得分:1)
在layoutInflater
的帮助下使用这样--------- TabSpec passSpec = tabs.newTabSpec("Pass Tab");
passSpec.setIndicator("Passengers", getResources().getDrawable(R.drawable.tab_message));
passSpec.setContent(new TabHost.TabContentFactory() {
public View createTabContent(String tag) {
View layout = mInflater.inflate(R.layout.tab_content_passengers, null);
return(layout);
}
});
tabs.addTab(passSpec);