【问题标题】:How to change the style of tab indicator (default one is an underline ) of tabbed activity in android?如何更改android中选项卡式活动的选项卡指示器的样式(默认为下划线)?
【发布时间】:2017-07-10 22:16:47
【问题描述】:

please click here to view the picture

上图是从 SYMPTOMATE 应用程序截取的屏幕截图。我想创建一个与该图片所示相同的选项卡式活动。随着标签的滑动,每个圆圈都会相应地突出显示。请提出一种自定义标签指示器的方法。

【问题讨论】:

    标签: android android-tabs


    【解决方案1】:

    对于您的自定义 TabLayout,您需要编写自定义资源。特别是你需要这个drawables。

    1. 选项卡指示器默认状态(indicator_default.xml)

      <?xml version="1.0" encoding="utf-8"?>
      <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
      <item>
          <shape
             android:innerRadius="0dp"
             android:shape="ring"
             android:thickness="5dp"
             android:useLevel="false">
             <solid android:color="@android:color/darker_gray"/>
         </shape>
      

    2. 选项卡指示器选中状态(indicator_selected.xml)

      <?xml version="1.0" encoding="utf-8"?>
      <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
      <item>
          <shape
              android:innerRadius="0dp"
              android:shape="ring"
              android:thickness="8dp"
              android:useLevel="false">
              <solid android:color="@color/colorPrimary"/>
          </shape>
      </item>
      

    3. 选项卡指示器选择器(indicator_selector.xml)

      <?xml version="1.0" encoding="utf-8"?>
      <selector xmlns:android="http://schemas.android.com/apk/res/android">
      
         <item android:drawable="@drawable/indicator_selected"
            android:state_selected="true"/>
      
         <item android:drawable="@drawable/indicator_default"/>
      </selector>
      

    最后,您应该在布局中添加 TablLayout,定义选项卡背景,如下所示

    <android.support.design.widget.TabLayout
        android:id="@+id/tablayout"
        android:layout_width="match_parent"
        android:layout_height="
        app:tabBackground="@drawable/tab_selector"
        app:tabGravity="center"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-06
      • 2019-05-17
      • 1970-01-01
      相关资源
      最近更新 更多