【问题标题】:TabHost indicator color change code not workingTabHost 指示器颜色更改代码不起作用
【发布时间】:2014-05-03 05:54:27
【问题描述】:

我一直在阅读 Stack 以尝试更改指示器的颜色(选项卡的下划线告诉您选择了哪个选项卡),但我使用了找到的代码 here,但它不起作用。我收到 2 个错误:

  1. view.findViewById(R.id.tabhost) 中,视图和tabhost 为红色(无法解析符号)。

  2. 一个在widget.getChildCount(),表示方法调用可能产生空指针异常。

  3. 第三个错误是在v.setBackgroundResource(R.drawable.tab_selector_color); 它说的一样,它可能会产生一个空指针异常。

我下载了所有的drawables并按照上面的链接改变了它们的颜色,然后我把它们都放在了我的drawable-mdpi文件夹中,就像它建议的那样。

我将发布我的主要活动的一部分,其中包含有错误的代码块,以及位于我的可绘制文件夹中的tab_selector_color.xml(我认为它必须放在这里,而不是布局文件夹,因为它有selector 标签)。

如何解决这些错误?提前致谢,如果有任何不清楚或需要查看更多文件,请告诉我。

我的主要活动中出现错误的方法:

(我使用了下面的标题代码,因为我在任何地方都没有TextView(使用图标作为我的指标可绘制部分),但无论该代码是否存在,3 个错误仍然存​​在)。

public void changeTabIndicators() {
        FragmentTabHost host = (FragmentTabHost)view.findViewById(R.id.tabhost);
        TabWidget widget = host.getTabWidget();
        for(int i = 0; i < widget.getChildCount(); i++) {
            View v = widget.getChildAt(i);

//            // Look for the title view to ensure this is an indicator and not a divider.
//            TextView tv = (TextView)v.findViewById(android.R.id.title);
//            if(tv == null) {
//                continue;
//            }
            v.setBackgroundResource(R.drawable.tab_selector_color);
        }
    }

tab_selector_color.xml

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Non focused states -->
    <item android:state_focused="false"
        android:state_selected="false"
        android:state_pressed="false"
        android:drawable="@drawable/tab_unselected_holo" />

    <item android:state_focused="false"
        android:state_selected="true"
        android:state_pressed="false"
        android:drawable="@drawable/tab_selected_holo" />

    <!-- Focused states -->
    <item android:state_focused="true"
        android:state_selected="false"
        android:state_pressed="false"
        android:drawable="@drawable/tab_unselected_focused_holo" />

    <item android:state_focused="true"
        android:state_selected="true"
        android:state_pressed="false"
        android:drawable="@drawable/tab_selected_focused_holo" />

    <!-- Pressed -->
    <!--    Non focused states -->
    <item android:state_focused="false"
        android:state_selected="false"
        android:state_pressed="true"
        android:drawable="@drawable/tab_unselected_pressed_holo" />

    <item android:state_focused="false"
        android:state_selected="true"
        android:state_pressed="true"
        android:drawable="@drawable/tab_selected_pressed_holo" />

    <!--    Focused states -->
    <item android:state_focused="true"
        android:state_selected="false"
        android:state_pressed="true"
        android:drawable="@drawable/tab_unselected_pressed_holo" />

    <item android:state_focused="true"
        android:state_selected="true"
        android:state_pressed="true"
        android:drawable="@drawable/tab_selected_pressed_holo" />

</selector>

【问题讨论】:

    标签: android indicator fragment-tab-host


    【解决方案1】:

    实际上黄色警告(#2 和 3 上的错误)并没有导致问题,但我所做的更改是 .getChildCount() 方法。我在TabWidget Android pages 中查找并在那里找不到此方法,因此我使用getTabCount() 代替。为了从view.findViewById(R.id.tabhost) 中修复单词view 中的错误,我删除了view. 并修复了它。

    我还删除了 TextView 代码,因为我的选项卡没有使用文本,而是小选项卡部分中的图标,所以我不需要整个方法(你可以看到它在我的代码中被注释掉了) .

    所以我想再次确认,TabWidget current tab bottom line color 确实对我有用,尽管从 Github 下载可绘制对象需要很长时间,因为你必须下载整个父文件夹......但我做到了,然后选择了拿出我需要的,把它们放在正确的文件夹中,它就起作用了!另请注意,您必须将选项卡指示器 xml(在答案中给出)放入您的可绘制文件夹中,因为这是每当 xml 具有 &lt;selector&gt; 标签时的约定。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-21
      • 2012-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多