欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android TabLayout選項(xiàng)卡使用教程

 更新時(shí)間:2023年04月06日 09:49:34   作者:別偷我的豬_09  
這篇文章主要介紹了Android TabLayout選項(xiàng)卡使用,為什么會(huì)有這篇文章呢,是因?yàn)橹瓣P(guān)于TabLayout的使用陸陸續(xù)續(xù)也寫(xiě)了好幾篇了,感覺(jué)比較分散,且不成體系,寫(xiě)這篇文章的目的就是希望能把各種效果的實(shí)現(xiàn)一次性講齊

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)文章

最新評(píng)論