MaterialDesign系列文章(七)TabLayout的使用

不怕跌倒,所以飛翔

TabLayout的使用

用于展示Tab的控件

1基本的使用方式

  • 通過xml設置

        <android.support.design.widget.TabLayout
            android:id="@+id/tl"
            android:layout_width="match_parent"
            android:layout_height="45dp">
    
            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Tab1"/>
    
            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Tab2"/>
    
            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Tab3"/>
    
        </android.support.design.widget.TabLayout>
    
  • 通過代碼設置條目

    tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
    
  • 一些基本參數設置

    • app:tabSelectedTextColor 設置選中字體的顏色
    • app:tabTextColor 未選中字體的顏色
    • app:tabIndicatorColor 設置指示器下標的顏色
    • app:tabBackground 整個TabLayout的顏色
    • app:tabIndicatorHeight 設置指示器下標的高度(這里如果設置成0的時候就是沒有底部的下劃線了)
    • app:tabMode Tab的模式(后面會詳細說明)
    • app:tabPadding 設置Tab內部的子控件的Padding
    • app:tabGravity 內容的顯示模式

2改變TabLayout的內部字體大小

app:tabTextAppearance="@android:style/TextAppearance.Holo.Large"http://設置文字的外貌

3添加圖標

tabLayout.addTab(tabLayout.newTab().setText("Tab 1").setIcon(R.mipmap.ic_launcher));

4Tab模式

  • fixed 固定的,標簽多的時候會被擠壓,并且不能滑動
  • scrollable 非固定的,標簽多的時候能滑動

5TabLayout的監聽(addOnTabSelectedListener)

  • onTabSelected(TabLayout.Tab tab) 選中了Tab的邏輯
  • onTabUnselected(TabLayout.Tab tab) 未選中Tab的邏輯
  • onTabReselected(TabLayout.Tab tab) 再次選中Tab的邏輯

6和ViewPager聯動

tabLayout.setupWithViewPager(Viewpager); (這里注意一點,當和ViewPager聯動的時候要重寫getPageTitle這個方法設置Tab的標題)

7默認選中某項

tabLayout.getTabAt(position).select();


這一系列文章的地址,希望對大家有幫助

項目地址

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容