【发布时间】:2014-12-09 04:44:33
【问题描述】:
我正在使用一个 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>
【问题讨论】:
-
不明白为什么标签变得如此困难。安卓把这件事搞砸了。为什么我不能只添加文本或仅使用 XML 为选项卡使用图标?为什么我必须编写代码并设置指标和创建选择器?有比 TabHost 或 FragmentTabHost 更好的选择吗?
标签: android xml android-layout android-tabhost android-tabs