Android TabHost 如何改变下划线样式

最近项目开发,无意中用到TabHost作为分栏展示,可是要调整选中栏下划线颜色,居然难倒了,网上找了一遍还是没有收获,这里记录一下,给相同问题的人一点参考;

 // tab栏初始化
        TabHost tab = (TabHost) findViewById(android.R.id.tabhost);
        //初始化TabHost容器
        tab.setup();
        //在TabHost创建标签,然后设置:标题/图标/标签页布局
        tab.addTab(tab.newTabSpec("tab1").setIndicator("人员配置" , null).setContent(R.id.tab1));
        tab.addTab(tab.newTabSpec("tab2").setIndicator("地图配置" , null).setContent(R.id.tab2));

        TabWidget tabWidget = tab.getTabWidget();//获取TabHost的头部

//循环每个tabView
for (int i=0; i<tabWidget.getChildCount(); i++){
    //获取tabView项
    View view = tabWidget.getChildAt(i);
    view.setBackgroundResource(R.drawable.menu_selector);

}

menu_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 定义按钮按下时的图片 -->
    <item android:drawable="@drawable/border_bottom_blue" android:state_selected="true"/>
    <!-- 定义按钮默认的图片 -->
    <item android:drawable="@drawable/border_bottom_gray"/>
</selector>

核心border_bottom_blue.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:top="-5dp"
        android:right="-5dp"
        android:left="-5dp">
        <shape>
            <solid android:color="@android:color/transparent"/>
            <stroke
                android:width="4dp"
                android:color="@color/colorPrimaryDark"/>
        </shape>
    </item>

</layer-list>

简单的记录下,希望能帮到你 

猜你喜欢

转载自blog.csdn.net/zhuc_dongyc/article/details/87857137
今日推荐