Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android中向布局XML文件中的TabLayout添加选项卡?_Android_Tabs - Fatal编程技术网

如何在Android中向布局XML文件中的TabLayout添加选项卡?

如何在Android中向布局XML文件中的TabLayout添加选项卡?,android,tabs,Android,Tabs,我学习安卓已经两周了。现在我正在开发我的第一个Android项目 在我的项目中,我需要使用标签和操作栏。我知道这在Android中很常见。对于操作栏,我使用v7的工具栏。我真的很了解动作吧。但是我对选项卡布局有一个问题。问题是我想将选项卡添加到布局XML文件中的选项卡布局中 例如: <TabLayout> //tab buttton goes here <TabButton1></TabButton1> <TabButton1></Tab

我学习安卓已经两周了。现在我正在开发我的第一个Android项目

在我的项目中,我需要使用标签和操作栏。我知道这在Android中很常见。对于操作栏,我使用v7的工具栏。我真的很了解动作吧。但是我对选项卡布局有一个问题。问题是我想将选项卡添加到布局XML文件中的选项卡布局中

例如:

<TabLayout>
//tab buttton goes here
 <TabButton1></TabButton1>
 <TabButton1></TabButton1>
 .
 .
<TabLayout>

//塔布·巴特顿在这里
.
.

我搜索了很多关于tablayout的教程。但所有教程都以编程方式为选项卡布局定义选项卡。如何在XML中为选项卡布局定义选项卡,并通过findViewById以编程方式为它们定义事件?我不想使用按钮视图而不是选项卡。

这应该是您想要的:

<android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_gravity="bottom"
            android:background="?attr/colorPrimary"
            app:tabMode="fixed">

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Popular" />

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Most Rated" />

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Favorites" />
        </android.support.design.widget.TabLayout>


您可以为每个选项卡指定一个布局xml,这可能是最接近您需要的布局xml。请参见此处:它以编程方式添加选项卡。对吧?我刚更新了我的问题。请检查我想要的xml。是的,没有办法。现在有一个新的包:com.google.android.material.tabs.TabItem