shape与selector配合使用实现Tab下划线效果

在使用Tabhost和ViewPager实现tab翻页的时候,需要在tab中加入下划线,效果如图

这里不打算用图片当背景,而是通过shape来画下划线。

对于一个tab的布局,

<RelativeLayout>
    <TextView />
    <View
        android:width="match_parent"
        android:height="2dp"
     android:background="@drawable/selector_tab_line"
/> </RelativeLayout>

以下是selector_tab_line.xml文件的实现,其中含shape的实现

<selector>
    <item android:state_selected="true">
              <shape>
                        <stroke android:width="1dp"    android:color="@color/tab_selected"/>   <!-- 起初没有使用stroke,直接使用solid,并不能画出一条直线,所以得加上这一标签 -->
                        <solid  android:color="@color/tab_selected"/>
              </shape>
    </item>

     <item android:state_selected="true">
               <shape>
                        <stroke android:width="1dp"           android:color="@color/tab_normal"/>
                        <solid  android:color="@color/tab_normal"/>
              </shape>
    </item>
</selector>
原文地址:https://www.cnblogs.com/ivan-aldrich/p/4921001.html