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

別費(fèi)勁了输玷,用第三方庫(kù)! SmartTabLayout

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

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

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中并沒(méi)有設(shè)置文本選中和未選中Tab時(shí)的效果返敬,我們可以通過(guò)app:stl_customTabTextLayoutId屬性引入自定義設(shè)置。

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
在這里設(shè)置選中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. 因?yàn)橐昧俗远xTab布局,所以原生布局帶有的Tab點(diǎn)擊效果消失(類似于水波紋)秸谢。

我們可以在 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. 簡(jiǎn)單的SmartTabLayout布局

但是選項(xiàng)卡沒(méi)有定義選中后的顏色,必須自定義選項(xiàng)卡才可以

<?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>

效果圖


簡(jiǎn)單smartTabLayout.gif
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末估蹄,一起剝皮案震驚了整個(gè)濱河市塑煎,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌臭蚁,老刑警劉巖最铁,帶你破解...
    沈念sama閱讀 216,919評(píng)論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異垮兑,居然都是意外死亡冷尉,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,567評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門甥角,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)网严,“玉大人,你說(shuō)我怎么就攤上這事嗤无≌鹗” “怎么了?”我有些...
    開封第一講書人閱讀 163,316評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵当犯,是天一觀的道長(zhǎng)垢村。 經(jīng)常有香客問(wèn)我,道長(zhǎng)嚎卫,這世上最難降的妖魔是什么嘉栓? 我笑而不...
    開封第一講書人閱讀 58,294評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮拓诸,結(jié)果婚禮上侵佃,老公的妹妹穿的比我還像新娘。我一直安慰自己奠支,他們只是感情好馋辈,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,318評(píng)論 6 390
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著倍谜,像睡著了一般迈螟。 火紅的嫁衣襯著肌膚如雪叉抡。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,245評(píng)論 1 299
  • 那天答毫,我揣著相機(jī)與錄音褥民,去河邊找鬼。 笑死洗搂,一個(gè)胖子當(dāng)著我的面吹牛消返,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播蚕脏,決...
    沈念sama閱讀 40,120評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼侦副,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了驼鞭?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,964評(píng)論 0 275
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤尺碰,失蹤者是張志新(化名)和其女友劉穎挣棕,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體亲桥,經(jīng)...
    沈念sama閱讀 45,376評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡洛心,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,592評(píng)論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了题篷。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片词身。...
    茶點(diǎn)故事閱讀 39,764評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖番枚,靈堂內(nèi)的尸體忽然破棺而出法严,到底是詐尸還是另有隱情,我是刑警寧澤葫笼,帶...
    沈念sama閱讀 35,460評(píng)論 5 344
  • 正文 年R本政府宣布深啤,位于F島的核電站,受9級(jí)特大地震影響路星,放射性物質(zhì)發(fā)生泄漏溯街。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,070評(píng)論 3 327
  • 文/蒙蒙 一洋丐、第九天 我趴在偏房一處隱蔽的房頂上張望呈昔。 院中可真熱鬧,春花似錦友绝、人聲如沸堤尾。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,697評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)哀峻。三九已至涡相,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間剩蟀,已是汗流浹背催蝗。 一陣腳步聲響...
    開封第一講書人閱讀 32,846評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留育特,地道東北人丙号。 一個(gè)月前我還...
    沈念sama閱讀 47,819評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像缰冤,于是被迫代替她去往敵國(guó)和親犬缨。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,665評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • 以前趁藿看到辦公室的同事怀薛,買花,訂花還特地買一個(gè)大花瓶配這些鮮花迷郑!每次看到她們喜滋滋地裁剪枝恋、插花,我總在心里想:干嘛...
    溫潤(rùn)如水閱讀 677評(píng)論 0 5
  • 有句很金典的話形容大學(xué)四年:“大一不知道自己不知道嗡害,大二知道自己不知道焚碌,大三不知道自己知道,大四知道自己知道”霸妹。上...
    冰沁雪閱讀 493評(píng)論 0 3