WanAndroid 项目页面Tab栏

2019-08-09  本文已影响0人  龙_君

开局先看效果:

tab.png

接下来上代码:

XML布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".ui.wan.fragment.fragment.Wan_projectFragment">

    <RelativeLayout
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_margin="5dp"
        android:background="@drawable/tab_background_ridus">



        <com.google.android.material.tabs.TabLayout
            android:id="@+id/wan_project_tab"
            app:tabMode="scrollable"
            app:tabBackground="@drawable/tab_background"
            android:background="#FFFFFF"
            android:layout_width="match_parent"
            app:tabSelectedTextColor="#FFFFFF"
            app:tabTextColor="#D8D8D8"
            app:tabIndicatorColor="#FFFFFF"
            app:tabRippleColor="#B5DCFF"
            android:layout_height="@dimen/x100"/>
    </RelativeLayout>




    <androidx.viewpager.widget.ViewPager
        android:id="@+id/wan_project_vp"
        android:layout_below="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>








    <ProgressBar
        android:id="@+id/wan_project_pb"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_width="@dimen/x100"
        android:layout_height="@dimen/x100" />


</RelativeLayout>
tab_background_ridus:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!-- 填充 -->
    <solid android:color="#FFF" />
    <!-- 圆角 -->
    <corners android:radius="5dp" />
    <stroke
        android:width="1.5dp"
        android:color="#FF5722" />
</shape>


tab_background:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/tab_background_unchecked"
        android:state_selected="false" />
    <item android:drawable="@drawable/tab_background_checked"
        android:state_selected="true" />
</selector>
tab_background_unchecked:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!-- 填充 -->
    <solid android:color="#FFFFFF" />
    <!-- 圆角,未选中的必须得设置,否则显示不完全 -->
    <corners
        android:radius="5dp" />
</shape>
tab_background_checked:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <!-- 填充 -->
    <solid android:color="#FF5722" />
    <!-- 圆角 去除 -->
    <corners android:radius="3dp" />



</shape>
然后在代码里添加:
 //给TabLayout 的 iten 之间添加一条分割线
        LinearLayout linearLayout = (LinearLayout) wanProjectTab.getChildAt(0);
        linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        linearLayout.setDividerDrawable(ContextCompat.getDrawable(BaseApp.getContext(),
                R.drawable.f));

到这就大功告成了!!!

tab.png

对你有帮助的话,别忘了点赞哦!

上一篇 下一篇

猜你喜欢

热点阅读