【问题标题】:Can't customized the android icon and text in a tab无法在选项卡中自定义 android 图标和文本
【发布时间】: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


【解决方案1】:

请查看以下链接中提供的演示实现。我相信它会解决你的问题:)

http://maxalley.wordpress.com/2012/10/27/android-styling-the-tabs-in-a-tabwidget/

它提供了自定义 TabWidget 的实现。
我希望这有帮助。

【讨论】:

    【解决方案2】:

    查看此示例项目,它向您展示了如何更改颜色以及您可以在其他示例中找到的其他内容。 Click here

    希望对你有帮助:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-06
      相关资源
      最近更新 更多