我正在使用android tabhost,问题是,我想自定义标签的文本和图标,但是在我的尝试中,结果也只是一个图标,并且也无法点击。我的代码有什么问题吗?感谢您的帮助
tabHost.setup(ctx, getSupportFragmentManager(), R.id.realtabcontent);
for (String tag : tags) {
if (tag.equals("home")) {
tabIndicator = LayoutInflater.from(ctx).inflate(R.layout.custom_tab, tabHost.getTabWidget(), false);
((TextView) tabIndicator.findViewById(R.id.icon_txt)).setText("news");
((ImageView) tabIndicator.findViewById(R.id.icon)).setImageResource(R.drawable.test_menu);
tabHost.addTab(tabHost.newTabSpec(tag).setIndicator(tabIndicator),Home.class, null);
} else if (tag.equals("news")) {
tabIndicator = LayoutInflater.from(ctx).inflate(R.layout.custom_tab, tabHost.getTabWidget(), false);
((TextView) tabIndicator.findViewById(R.id.icon_txt)).setText("news");
((ImageView) tabIndicator.findViewById(R.id.icon)).setImageResource(R.drawable.test_menu);
tabHost.addTab(tabHost.newTabSpec(tag).setIndicator(tabIndicator),NewsFragment.class, null);
} else if (tag.equals("info")) {
tabIndicator = LayoutInflater.from(ctx).inflate(R.layout.custom_tab, tabHost.getTabWidget(), false);
((TextView) tabIndicator.findViewById(R.id.icon_txt)).setText("news");
((ImageView) tabIndicator.findViewById(R.id.icon)).setImageResource(R.drawable.test_menu);
tabHost.addTab(tabHost.newTabSpec(tag).setIndicator(tabIndicator),InfoFragment.class, null);
} else {
tabIndicator = LayoutInflater.from(ctx).inflate(R.layout.custom_tab, tabHost.getTabWidget(), false);
((TextView) tabIndicator.findViewById(R.id.icon_txt)).setText("news");
((ImageView) tabIndicator.findViewById(R.id.icon)).setImageResource(R.drawable.test_menu);
tabHost.addTab(tabHost.newTabSpec(tag).setIndicator(tabIndicator),PageFragment.class, null);
}
}
tabHost.getTabWidget().setDividerDrawable(null);
和xml来定制选项卡
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/icon_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp" />
</LinearLayout>
请查看以下链接中提供的已演示的实现。我相信它将解决您的问题:)
http://maxalley.wordpress.com/2012/10/27/android-styling-the-tabs-in-a-tabwidget/
它提供了自定义TabWidget的实现。
我希望这有帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句