【问题标题】:How to add tabs to TabLayout in layout XML file in Android?如何在 Android 的布局 XML 文件中将选项卡添加到 TabLayout?
【发布时间】:2016-02-05 20:53:39
【问题描述】:

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

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

例如:

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

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

【问题讨论】:

  • 您可以为每个选项卡指定一个布局xml,这可能是您可以获得的最接近您想要的内容。见这里:stackoverflow.com/questions/31698756/…
  • 它以编程方式添加选项卡。对吗?
  • 我刚刚更新了我的问题。请检查我想要的xml。
  • 是的,没办法。

标签: android tabs


【解决方案1】:

这应该是你想要的:

<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>

【讨论】:

  • 现在有一个新包:com.google.android.material.tabs.TabItem
猜你喜欢
  • 2011-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-27
  • 1970-01-01
  • 1970-01-01
  • 2021-03-28
相关资源
最近更新 更多