Appbarlayout 的 Tablayout

问题描述

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"

    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            app:tabMode="scrollable">

            <com.google.android.material.tabs.TabItem
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="Tab1" />

            <com.google.android.material.tabs.TabItem
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="Tab2" />

            <com.google.android.material.tabs.TabItem
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="Tab3" />

            <com.google.android.material.tabs.TabItem
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="Tab4" />

            <com.google.android.material.tabs.TabItem
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="Tab5" />

            <com.google.android.material.tabs.TabItem
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="Tab6" />

            <com.google.android.material.tabs.TabItem
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="Tab7" />

            <com.google.android.material.tabs.TabItem
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="Tab8" />

            <com.google.android.material.tabs.TabItem
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="Tab9" />

            <com.google.android.material.tabs.TabItem
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="Tab10" />

            <com.google.android.material.tabs.TabItem
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="Tab11" />

            <com.google.android.material.tabs.TabItem
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="Tab12" />
        </com.google.android.material.tabs.TabLayout>
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.core.widget.nestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".tabactivity" />
    </androidx.core.widget.nestedScrollView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

我早些时候从那个重复的 TabHost 中提出问题,我在 answer 中注意到 TabHost 没有被废弃。 TabLayout 已弃用。如果我使用TabLayout会更好吗?如果不是我应该使用的 TabLayout 的替代方法是什么?这是TabLayout。在 this question 中,AskNilesh 表示使用 com.google.android.material.tabs.TabLayout。但是,我们将它用于 TabLayout。我试图创建选项卡式活动

enter image description here

它还包含 TabLayout

解决方法

最好使用 TabLayout 只是因为 TabHost is in fact deprecated (而 TabLayout 不是)。查看文档,而不是问题...

该类在 API 级别 30 中已弃用。 新的应用程序应该使用片段 API 而不是这个类:使用 TabLayout 和 ViewPager。