【问题标题】:TabLayout Indicator customizationTabLayout 指标自定义
【发布时间】:2016-10-12 09:26:06
【问题描述】:

我一直在寻找如何将 Tablayout 中的指示器更改为圆形,像这样

但我不知道该怎么做,任何帮助!

【问题讨论】:

    标签: android android-tablayout


    【解决方案1】:

    从source code,标签指示器定义为:

    <style name="Base.Widget.Design.TabLayout" parent="android:Widget">
            <item name="tabMaxWidth">@dimen/design_tab_max_width</item>
            <item name="tabIndicatorColor">?attr/colorAccent</item>
            <item name="tabIndicatorHeight">2dp</item>
            <item name="tabPaddingStart">12dp</item>
            <item name="tabPaddingEnd">12dp</item>
            <item name="tabBackground">?attr/selectableItemBackground</item>
            <item name="tabTextAppearance">@style/TextAppearance.Design.Tab</item>
            <item name="tabSelectedTextColor">?android:textColorPrimary</item>
        </style>
    

    而tabIndicatorColor属性定义为:

    <declare-styleable name="TabLayout">
            <attr name="tabIndicatorColor" format="color"/>
            <attr name="tabIndicatorHeight" format="dimension"/>
    

    所以我相信你不能把它改成可绘制的(形状),你只能改变它的颜色。

    另一种方法是为选项卡定义自定义视图并自己处理指示器状态

    【讨论】:

      【解决方案2】:

      1) 为您的选项卡状态创建布局(例如:tab_selected.xml 和 tab_unselected.xml)

      2) 使用布局为标签设置自定义视图:

      //get your tab item
      TabLayout.Tab tabItem = tabLayout.getTabAt(i);
      //inflate your layout to a view and set it as the tab's custom view
      tabItem.setCustomView(customView);
      

      3) 然后,使用setOnTabSelectedListener,继续监听选择了哪个选项卡并相应地更新其自定义视图(使用setCustomView)

      【讨论】:

      • 这会提供任何动画吗?
      • @user1278890 如果你的意思是从选中状态到未选中状态的动画,我不这么认为。
      【解决方案3】:

      我认为您应该为标签使用自定义视图并使用您的 TabLayout.OnTabSelectedListener 进行更改

      【讨论】:

        猜你喜欢
        • 2017-07-09
        • 2018-03-14
        • 1970-01-01
        • 2023-03-15
        • 2017-01-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多