【问题标题】:How do I remove the selected tab indicator from the TabWidget?如何从 TabWidget 中删除选定的选项卡指示器?
【发布时间】:2013-02-03 12:09:31
【问题描述】:

这是我要删除的内容:

如何替换显示当前显示哪个选项卡的指示器以及跨越整个选项卡小部件的蓝线?

要指定:我只想指出选择了哪个选项卡:

  • tab_button_active.9.png 应在选项卡被选中时显示为背景
  • tab_button_inactive.9.png 如果选项卡未被选中,则应显示为背景。

edit :将 tabStripEnabled 设置为 false 无效。向其添加样式并将“@android:style/Widget.Holo.ActionBar”作为其父级也是不可能的,因为我的目标是 API 级别 7,而 ActionBar 是在 API 级别 11 中实现的。

【问题讨论】:

标签: android


【解决方案1】:

我还有另一个技巧。

    TabLayout tabLayout = (TabLayout) fragmentView.findViewById(R.id.tabs);
    tabLayout.setSelectedTabIndicatorHeight(0);

这与 Eleojasmil 的想法基本相同。

【讨论】:

  • 现在 java 说 tabLayout.setSelectedTabIndicatorHeight(0) 方法已弃用。
【解决方案2】:

这一行app:tabIndicatorHeight="0dp"为我解决了xml中的问题

 <android.support.design.widget.TabLayout
        android:id="@+id/view_bottom_tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabIndicatorHeight="0dp"             //this line
        app:tabGravity="fill"
        app:tabMode="fixed" />

【讨论】:

    【解决方案3】:

    如果android:tabStripEnabled="false" 不起作用,那么我也假设调用setStripEnabled(boolean stripEnabled) 也不会起作用。如果所有这些都是真的,那么您的问题可能不在 TabWidget 中。

    我建议查看您的标签指示器。试试这些修改。这段代码取自一个有标签的片段。

    这是创建标签指示器视图的代码。

        View indicator = LayoutInflater.from(getActivity()).inflate(R.layout.tab,
    (ViewGroup) mRoot.findViewById(android.R.id.tabs), false);
    
            TabSpec tabSpec = mTabHost.newTabSpec(tag);
            tabSpec.setIndicator(indicator);
            tabSpec.setContent(tabContentId);
    

    您的标签指示器视图可能与此类似。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:background="@drawable/tabselector"
        android:padding="5dp" >
    
        <ImageView
            android:id="@+id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/tab1icon"/>
    
    </LinearLayout>
    

    现在这里重要的部分是 LinearLayout 中的android:background="@drawable/tabselector"。我的看起来像这样。

    <?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_light" />
        <item
            android:state_focused="false"
            android:state_selected="true"
            android:state_pressed="false"
            android:drawable="@drawable/tab_selected_light" />
        <!-- Focused states -->
        <item
            android:state_focused="true"
            android:state_selected="true"
            android:state_pressed="false"
            android:drawable="@drawable/tab_focused_light" />
        <!-- Pressed state -->
        <item
            android:state_pressed="true"
            android:drawable="@drawable/tab_pressed_light" />
    </selector>
    

    此 tabselector.xml 用于将 @drawable/tab_pressed_light@drawable/tab_button_active@drawable/tab_unselected_light@drawable/tab_button_inactive 交换

    请务必检查进入 tabselector.xml 的所有可绘制对象底部没有蓝色条带。当我查看您的图像时,我可以看到该条带上有 5px 的小间隙,这就是让我认为该条带不是来自您的 TabWidget 的原因。希望这会有所帮助。

    【讨论】:

    • android:tabStripEnabled="false" 对我不可用。我不得不使用 app:tabIndicatorHeight="0dp"
    【解决方案4】:

    对于TabLayout: 只需使用app:tabIndicatorColor="@android:color/transparent"

    <android.support.design.widget.TabLayout
            android:id="@+id/profile_album_tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:animateLayoutChanges="true"
            android:theme="@style/AppTheme.AppBarOverlay"
            app:tabBackground="@drawable/tab_background"
            app:tabIndicatorColor="@android:color/transparent"
            app:tabPadding="@dimen/common_margin" />
    

    【讨论】:

      【解决方案5】:

      将此添加到您的TabLayout XML

      app:tabIndicator="@null"
      

      【讨论】:

        【解决方案6】:

        只有一个答案 您只需将指示器的颜色更改为透明

        颜色.xml

         <color name="transparent2">#00000000</color>
        

        把这一行放到你的tabwidget

        tabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.transparent2));
        

        app:tabIndicatorColor="@color/transparent2"
        

        【讨论】:

        • 对于透明色:android.R.color.transparent
        【解决方案7】:

        尝试将 Tab 指示器高度设置为零:

        tabLayout.setSelectedTabIndicatorHeight(0);
        

        【讨论】:

        • 重复我的回答做得好 :)
        • 现已弃用
        【解决方案8】:

        使用tabLayout.setSelectedTabIndicator(0)

        tabLayout.setSelectedTabIndicatorHeight(0) 现在已被弃用,tabLayout.setSelectedTabIndicatorColor(Color.TRANSPARENT) 的性能也不会那么好,因为它使用了透明度。

        【讨论】:

          【解决方案9】:

          使用

              tabLayout.setSelectedTabIndicator(null);
          

          这会将选定的可绘制指标设置为 null。

          【讨论】:

            【解决方案10】:

            为您的TabWidget 设置属性android:tabStripEnabled="false"

            【讨论】:

            • 在 TabWidget 中设置 android:tabStripEnabled="false" 无效。
            • 哦..太糟糕了..试试android:tabStripEnabled="false" android:alpha="0"
            • 这将使整个tabwidget透明
            • 那我需要自己试一试,然后回复你。很抱歉让您失望了。
            【解决方案11】:

            找了半天把丑陋的holo菜单栏去掉 尝试了一切。出于另一个原因意外使用了此代码,感谢上帝将其删除。

                for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {          
                    tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#FF0000")); // unselected
                    TextView tv = (TextView)tabhost.getTabWidget().getChildAt(i).findViewById(android.R.id.title); //Unselected Tabs
                    tv.setTextColor(Color.parseColor("#ffffff"));
                }
            

            来源:Android tabhost change text color style

            【讨论】:

              【解决方案12】:

              你可以简单地在你的 tablayout 上使用这个方法来隐藏 tab 指示器。

              tabLayout.setSelectedTabIndicatorColor(Color.TRANSPARENT);
              

              【讨论】:

                【解决方案13】:

                使用这个app:tabIndicator="@null"

                <com.google.android.material.tabs.TabLayout
                    android:id="@+id/tab_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:tabGravity="fill"
                    app:tabIndicator="@null"
                    app:tabMode="fixed" />
                

                【讨论】:

                  【解决方案14】:

                  这是我使用的,因为 tabIndicator.setSelectedTabIndicatorHeight(0); 已弃用

                  使用tabIndicator.setSelectedTabIndicator(0);

                  【讨论】:

                    【解决方案15】:

                    app:tabIndicatorHeight="0dp" 为我工作的最佳设置。

                    【讨论】:

                      【解决方案16】:

                      我假设您正在使用 ActionBarSherlock。使用 TabWdiget 的问题在于,它不是 ABS 的一部分,因此对于每个 Android 版本来说,它看起来都是“原生的”。对我来说,使用 ABS 进行选项卡导航的最佳方法是使用 ViewPager 和 ViewPagerIndicator 并设置指示器的样式。缺点是,您的标签必须是片段。如果你需要你的标签成为活动,那么你应该看看 HoloEveryhwhere 的人是否设法添加了 TabWidget。

                      【讨论】:

                      • 我没有使用 ABS。只需 v4 支持库。
                      • 无论如何,我的方法不需要 ABS。我提到它是因为它适用于 ABS。
                      • 我认为您错过了为 OP 问题提供解决方案的部分。
                      【解决方案17】:
                      tabHost.getTabWidget().setStripEnabled(false);
                      tabHost.getTabWidget().getChildAt(1).setBackgroundColor(getResources().getColor(R.color.tabcolor));
                      tabHost.getTabWidget().getChildAt(2).setBackgroundColor(getResources().getColor(R.color.tabcolor));
                      

                      More info here

                      【讨论】:

                        猜你喜欢
                        • 1970-01-01
                        • 2021-06-06
                        • 1970-01-01
                        • 1970-01-01
                        • 2023-03-19
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        相关资源
                        最近更新 更多