Android TabLayout選項(xiàng)卡使用教程
TabLayout
TabLayout 在開(kāi)發(fā)中一般作為選項(xiàng)卡使用,常與 ViewPager2 和Fragment 結(jié)合起來(lái)使用。
常用屬性:
app:tabBackground 設(shè)置 TabLayout 的背景色,改變整個(gè)TabLayout 的顏色;
app:tabTextColor 設(shè)置未被選中時(shí)文字的顏色;
app:tabSelectorColor 設(shè)置選中時(shí)文字顏色;
app:tabTextAppearance="@android:style/TextAppearance.Large" 設(shè)置 TabLayout 的文本主題,無(wú)法通過(guò) textSize 來(lái)設(shè)置文字大小,只能通過(guò)主題來(lái)設(shè)定;
app:tabMode="scrollable"設(shè)置 TabLayout 可滑動(dòng),當(dāng) tabItem 個(gè)數(shù)較多時(shí),一個(gè)界面無(wú)法呈現(xiàn)所有的導(dǎo)航標(biāo)簽,此時(shí)就必須要用;
app:tabIndicator 設(shè)置指示器;
app:tabIndicatorColor 設(shè)置指示器顏色;
??????? app:tabIndecatorHeight 設(shè)置指示器高度,當(dāng)app:tabIndecatorHeight="0dp",隱藏 Indicator 效果;
app:tabTextAppearance="@android:style/TextAppearance.Holo.Large" 改變 TabLayout 里 TabItem 文字的大??;
app: tabPadding 設(shè)置 Tab 內(nèi)部 item 的 padding。也可以單獨(dú)設(shè)置某個(gè)方向的padding, 比如 app:tabPaddingStart 設(shè)置左邊距;
app:paddingEdng / app:paddingStart 設(shè)置整個(gè) TabLayout 的 padding;
app:tabGravity="center" 居中,如果是 fill,則充滿;
app:tabMaxWidth / app:tabMinWidth 設(shè)置最大/最小的 tab 寬度,對(duì) Tab 的寬度進(jìn)行限制。
TabItem
給TabLayout 添加 Item 有兩種方法,其中一種就是使用 TabItem 在 xml 里直接添加。
1. 使用TabItem 給 TabLayout 添加卡片。
<com.google.android.material.tabs.TabItem android:layout_width="wrap_content" android:layout_height="wrap_content" android:icon="@android:drawable/ic_menu_add" android:text="添加"/>
android:icon 設(shè)置圖標(biāo);
Android:text 設(shè)置文本;
2. 通過(guò)代碼添加。使用 TabLayoutMediator()
new TabLayoutMediator(binding.tab, binding.viewPager, new TabLayoutMediator.TabConfigurationStrategy() { @Override public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) { //TODO 設(shè)置卡片的文本/圖標(biāo) tab.setText(mTitles.get(position)) .setIcon(mIcons.get(position)); } }).attach();
其中 mTitles 和 mIcons 是存放 text 和 Icon 的list。效果如下:
可以看到 text 在英文狀態(tài)下默認(rèn)都是大寫(xiě),這是因?yàn)樵?TabLayout 的源碼中默認(rèn)設(shè)置屬性 textAllCaps=true。所以可以在 TabLayout 中設(shè)置如下屬性來(lái)改成小寫(xiě)。
app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget"
演示效果的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=".MainActivity"> <com.google.android.material.tabs.TabLayout android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="8dp"> <com.google.android.material.tabs.TabItem android:layout_width="wrap_content" android:layout_height="wrap_content" android:icon="@android:drawable/ic_menu_add" android:text="添加"/> <com.google.android.material.tabs.TabItem android:layout_width="wrap_content" android:layout_height="wrap_content" android:icon="@android:drawable/ic_delete" android:text="刪除"/> <com.google.android.material.tabs.TabItem android:layout_width="wrap_content" android:layout_height="wrap_content" android:icon="@android:drawable/ic_menu_camera" android:text="相機(jī)"/> </com.google.android.material.tabs.TabLayout> <com.google.android.material.tabs.TabLayout android:id="@+id/tabs1" style="@style/Widget.MaterialComponents.TabLayout" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabMode="scrollable" android:layout_margin="8dp"> <com.google.android.material.tabs.TabItem android:layout_width="wrap_content" android:layout_height="wrap_content" android:icon="@android:drawable/ic_menu_add" android:text="添加"/> <com.google.android.material.tabs.TabItem android:layout_width="wrap_content" android:layout_height="wrap_content" android:icon="@android:drawable/ic_delete" android:text="刪除"/> <com.google.android.material.tabs.TabItem android:layout_width="wrap_content" android:layout_height="wrap_content" android:icon="@android:drawable/ic_menu_camera" android:text="相機(jī)"/> <com.google.android.material.tabs.TabItem android:layout_width="wrap_content" android:layout_height="wrap_content" android:icon="@android:drawable/ic_menu_add" android:text="添加"/> <com.google.android.material.tabs.TabItem android:layout_width="wrap_content" android:layout_height="wrap_content" android:icon="@android:drawable/ic_delete" android:text="刪除"/> <com.google.android.material.tabs.TabItem android:layout_width="wrap_content" android:layout_height="wrap_content" android:icon="@android:drawable/ic_menu_camera" android:text="相機(jī)"/> </com.google.android.material.tabs.TabLayout> <com.google.android.material.tabs.TabLayout android:id="@+id/tabs2" style="@style/Widget.MaterialComponents.TabLayout.Colored" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabIndicatorColor="@color/purple_700" android:layout_margin="8dp"> <com.google.android.material.tabs.TabItem android:layout_width="wrap_content" android:layout_height="wrap_content" android:icon="@android:drawable/ic_menu_add" android:text="添加"/> <com.google.android.material.tabs.TabItem android:layout_width="wrap_content" android:layout_height="wrap_content" android:icon="@android:drawable/ic_delete" android:text="刪除"/> <com.google.android.material.tabs.TabItem android:layout_width="wrap_content" android:layout_height="wrap_content" android:icon="@android:drawable/ic_menu_camera" android:text="相機(jī)"/> </com.google.android.material.tabs.TabLayout> <com.google.android.material.tabs.TabLayout android:layout_margin="8dp" android:id="@+id/tabs3" style="@style/Widget.Design.TabLayout" android:layout_width="match_parent" android:layout_height="wrap_content"> <com.google.android.material.tabs.TabItem android:layout_width="wrap_content" android:layout_height="wrap_content" android:icon="@android:drawable/ic_menu_add" android:text="添加" /> <com.google.android.material.tabs.TabItem android:layout_width="wrap_content" android:layout_height="wrap_content" android:icon="@android:drawable/ic_menu_call" android:text="刪除" /> <com.google.android.material.tabs.TabItem android:layout_width="wrap_content" android:layout_height="wrap_content" android:icon="@android:drawable/ic_menu_camera" android:text="菜單" /> </com.google.android.material.tabs.TabLayout> <com.google.android.material.tabs.TabLayout android:id="@+id/tabs4" app:tabTextAppearance="@android:style/TextAppearance.Holo.Large" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabIndicatorHeight="0dp" android:layout_margin="8dp"> <com.google.android.material.tabs.TabItem android:layout_width="wrap_content" android:layout_height="wrap_content" android:icon="@android:drawable/ic_menu_add" android:text="add"/> <com.google.android.material.tabs.TabItem android:layout_width="wrap_content" android:layout_height="wrap_content" android:icon="@android:drawable/ic_delete" android:text="刪除"/> <com.google.android.material.tabs.TabItem android:layout_width="wrap_content" android:layout_height="wrap_content" android:icon="@android:drawable/ic_menu_camera" android:text="相機(jī)"/> </com.google.android.material.tabs.TabLayout> </LinearLayout>
到此這篇關(guān)于Android TabLayout選項(xiàng)卡使用教程的文章就介紹到這了,更多相關(guān)Android TabLayout內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android仿zaker用手向上推動(dòng)的特效開(kāi)發(fā)【推動(dòng)門(mén)效果】(附demo源碼下載)
這篇文章主要介紹了Android仿zaker用手向上推動(dòng)的特效,結(jié)合完整實(shí)例形式分析了Android滑動(dòng)切換效果的實(shí)現(xiàn)步驟與相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07appium運(yùn)行各種坑爹報(bào)錯(cuò)問(wèn)題及解決方法【推薦】
這篇文章主要介紹了 appium運(yùn)行各種坑爹報(bào)錯(cuò)問(wèn)題及解決方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06Android Studio 超級(jí)簡(jiǎn)單的打包生成apk的方法
本篇文章主要介紹了Android Studio 超級(jí)簡(jiǎn)單的打包生成apk的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10Android自定義水平進(jìn)度條的圓角進(jìn)度
這篇文章主要為大家詳細(xì)介紹了Android自定義水平進(jìn)度條的圓角進(jìn)度,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08Android+Flutter實(shí)現(xiàn)文字跑馬燈特效
跑馬燈常常被運(yùn)用在很多領(lǐng)域, 例如商場(chǎng)的電子條幅、大樓的宣傳廣告位、地鐵的廣告位。今天我們來(lái)說(shuō)一下flutter 怎么通過(guò)繪制來(lái)實(shí)現(xiàn)跑馬燈效果!,希望對(duì)大家有所幫助2022-11-11Android 多圖上傳后將圖片進(jìn)行九宮格展示的實(shí)例代碼
這篇文章主要介紹了Android 多圖上傳后將圖片進(jìn)行九宮格展示,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11Kotlin實(shí)現(xiàn)在類(lèi)里面創(chuàng)建main函數(shù)
這篇文章主要介紹了Kotlin實(shí)現(xiàn)在類(lèi)里面創(chuàng)建main函數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03Android開(kāi)發(fā)之使用GridView展示圖片的方法
這篇文章主要介紹了Android開(kāi)發(fā)之使用GridView展示圖片的方法,涉及Android使用GridView操作圖片的布局與圖片調(diào)用功能實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-01-01