如何为标签添加自定义布局?例如,我想在标签中添加图标和标题。和自定义活动/非活动颜色。代码:
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
mViewPager = (ViewPager) findViewById(R.id.pager);
mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager);
TabSpec tab1spec = mTabHost.newTabSpec("tab1").setIndicator("Tab 1");
TabSpec tab2spec = mTabHost.newTabSpec("tab2").setIndicator("Tab 2");
mTabsAdapter.addTab(tab1spec, ConverterFragment.class, savedInstanceState);
mTabsAdapter.addTab(tab2spec, RatesFragment.class, savedInstanceState);
if (savedInstanceState != null) {
mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
}
答案 0 :(得分:0)
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent = new Intent().setClass(this, YourClass.class);
spec = tabHost.newTabSpec("home").setIndicator("Home",
res.getDrawable(R.drawable.tab_options_home))
.setContent(intent);
tabHost.addTab(spec);
使用上述代码,您应该能够创建选项卡,关联意图以启动活动并为选项卡设置图标(第5行,“主页”将是标题选项卡)。该图标将根据选项卡是处于活动状态还是非活动状态而更改,但为此您需要定义文件“tab_options_home.xml”(或任何您想要的名称,但请记住在代码中更改它)并将其保存到{ {1}}文件夹。该文件应如下所示:
drawable
使用此xml,您可以定义选项卡处于活动状态(<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Icon used when selected -->
<item android:drawable="@drawable/hover_icon"
android:state_selected="true" />
<!-- Icon used when not selected -->
<item android:drawable="@drawable/regular_icon" />
)或非活动状态时应使用的图标。我知道为时已晚,但希望它可以帮助任何需要它的人
PS:记得延长state_selected="true"