TabLayout 的指示器(indicator)修改寬度

別費勁了,用第三方庫! SmartTabLayout

使用支持更多自定義的SmartTabLayout.有我們想要的自定義下劃線寬度屬性。

屬性 描述
stl_indicatorAlwaysInCenter 如果設置為true,選中的標簽總是顯示在中心(如報刊亭的谷歌應用程序),默認為false
stl_indicatorWithoutPadding 如果設置為真,畫出沒有填充標簽的指標,默認為假
stl_indicatorInFront 在前面的下劃線,默認的假畫
stl_indicatorInterpolation 指標的行為:: ‘linear’ or ‘smart’
stl_indicatorGravity 指示器的位置: ‘bottom’ or ‘top’ or ‘center’, default ‘bottom’
stl_indicatorColor 指示劑顏色
stl_indicatorColors 該指標的多個顏色,可以設置每個標簽的顏色
stl_indicatorThickness 指標的厚度
stl_indicatorWidth 指標的寬度(width), default ‘a(chǎn)uto’
stl_indicatorCornerRadius 圓角半徑的指示器
stl_overlineColor 頂線的顏色
stl_overlineThickness 頂線厚度
stl_underlineColor 底線的顏色
stl_underlineThickness 底線的厚度
stl_dividerColor 標簽的顏色之間的分隔
stl_dividerColors 制表符分隔的多個顏色,可以設置每個標簽的顏色
stl_dividerThickness 間隔(divider)的厚度
stl_defaultTabBackground 背景中每個選項卡。一般來說,設置statelistdrawable
stl_defaultTabTextAllCaps 如果設置為真,所有標簽的標題將是大寫的,default true
stl_defaultTabTextColor 默認的選項卡的文本顏色
stl_defaultTabTextSize 默認的選項卡的文本大小
stl_defaultTabTextHorizontalPadding 默認情況下包含的選項卡的文本布局填充
stl_defaultTabTextMinWidth tab最小寬度
stl_customTabTextLayoutId 布局標識自定義選項卡。如果不指定布局,使用默認選項卡
stl_customTabTextViewId 自定義選項卡布局中的文本視圖標識。如果你不確定customtabtextlayoutid,不工作
stl_distributeEvenly 如果設置為真,每個標簽都有相同的權重, default false
stl_clickable 如果設置為假,請禁用選項卡的選擇, default true
stl_titleOffset 如果設置為“auto_center,滑塊位置的標簽中會不斷向中心。如果指定一個維度將它從左邊偏移,默認24dp
stl_drawDecorationAfterTab Draw the decoration(indicator and lines) after drawing of tab, default false 繪制標簽后的裝飾(指標和線)

1. 添加依賴

    // smarttablayout
    implementation 'com.ogaclejapan.smarttablayout:library:1.6.1@aar'

2. xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".modifyTablayoutIndicator.MondifyActivity">


    <com.ogaclejapan.smarttablayout.SmartTabLayout
        android:id="@+id/smart_tablayout"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="@color/colorPrimary"
        app:stl_clickable="true"
        app:stl_customTabTextLayoutId="@layout/view_tab_text"
        app:stl_distributeEvenly="true"
        app:stl_dividerThickness="0dp"
        app:stl_indicatorColor="@color/colorAccent"
        app:stl_indicatorGravity="bottom"
        app:stl_indicatorInterpolation="linear"
        app:stl_indicatorThickness="2dp"
        app:stl_indicatorWidth="20dp"
        app:stl_overlineThickness="0dp"
        app:stl_titleOffset="auto_center"
        app:stl_underlineThickness="0dp">

    </com.ogaclejapan.smarttablayout.SmartTabLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


</LinearLayout>

3. SmartTabLayout中并沒有設置文本選中和未選中Tab時的效果,我們可以通過app:stl_customTabTextLayoutId屬性引入自定義設置。

view_tab_text.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tab_text"
    style="@style/Tabtext"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingLeft="20dp"
    android:paddingRight="20dp">
</TextView>

styles.xml

    <style name="Tabtext">
        <item name="android:textColor">@color/selector_tab_text</item>
        <item name="android:textSize">20dp</item>
    </style>

selector_tab_text.xml
在這里設置選中tab的字體顏色,和未選中tab的字體顏色。

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

4. 因為引用了自定義Tab布局,所以原生布局帶有的Tab點擊效果消失(類似于水波紋)。

我們可以在 view_tab_text.xml 文件中添加想要的水波紋效果。

 android:foreground="@drawable/ripple_app_color"

ripple_app_color.xml: 水波紋效果

<?xml version="1.0" encoding="utf-8" ?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#292421">
</ripple>
  1. Activity
public class MondifyActivity extends AppCompatActivity {

    private SmartTabLayout mSmartTablayout;
    private ViewPager mViewPager;
    private List<String> titles = new ArrayList<>();
    private List<Fragment> fragments = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mondify);
        initView();
        initSet();
    }

    private void initView() {
        mSmartTablayout = (SmartTabLayout) findViewById(R.id.smart_tablayout);
        mViewPager = (ViewPager) findViewById(R.id.viewPager);
    }

    private void initSet() {

        titles.add("aaaa");
        titles.add("bbbb");
        titles.add("cccc");
        titles.add("dddd");
        titles.add("eeee");
        titles.add("ffff");

        for (int i = 0; i < titles.size(); i++) {
            fragments.add(new BlankFragment());
        }

        MyAdapter adapter = new MyAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(adapter);
        mSmartTablayout.setViewPager(mViewPager);


    }

    class MyAdapter extends FragmentPagerAdapter {

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return titles.get(position);
        }
    }

}

6. 完成效果圖

自定義smartTabLayout.gif

7. 簡單的SmartTabLayout布局

但是選項卡沒有定義選中后的顏色,必須自定義選項卡才可以

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".modifyTablayoutIndicator.SmartActivity">

    <com.ogaclejapan.smarttablayout.SmartTabLayout
        android:id="@+id/smartTabLayout"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        app:stl_indicatorGravity="bottom"
        app:stl_indicatorInterpolation="smart"
        app:stl_indicatorThickness="2dp"
        app:stl_distributeEvenly="true"
        app:stl_dividerThickness="0dp"
        app:stl_indicatorColor="@color/colorPrimary"
        app:stl_indicatorWidth="20dp">


    </com.ogaclejapan.smarttablayout.SmartTabLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/vp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v4.view.ViewPager>


</LinearLayout>

效果圖


簡單smartTabLayout.gif
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 以前??吹睫k公室的同事,買花,訂花還特地買一個大花瓶配這些鮮花!每次看到她們喜滋滋地裁剪、插花,我總在心里想:干嘛...
    溫潤如水閱讀 734評論 0 5
  • 有句很金典的話形容大學四年:“大一不知道自己不知道,大二知道自己不知道,大三不知道自己知道,大四知道自己知道”。上...
    冰沁雪閱讀 553評論 0 3

友情鏈接更多精彩內(nèi)容