java tabitem,Android 基础[花式实现底部导航栏之TabLayout]

d9cf0fe02c435aec02889e4ad5f8a5f7.gif

前文中我们已经简单介绍了BottomNavigationView实现选项卡页面的细节,接下来我们继续花式实现底部导航栏之旅,本片中主要是使用TabLayout实现,TabLayout也是Support Design库中提供的控件。

TabLayout继承自HorizontalScrollView,可以用于展示多个选项卡,其灵活度较之BottomNavigationView更高,TabLayout的继承关系如下图:

f02898a02029e5d7915f10dc924abb6c.png

接下来我们将学习如何使用TabLayout实现一个选项卡页面。

创建TabLayout

使用TabLayout首先需要引入Support Design库,在此不再详细赘述,TabLayout与BottomNavigationView一样,继承自View,使其即可在XML中创建,也可以使用Java代码创建,在XML中声明TabLayout的代码如下:

android:id="@+id/tabs"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:minHeight="?attr/actionBarSize"

app:tabTextAppearance="@style/MyCustomTextAppearance"

app:tabIndicatorHeight="0dp"

app:tabMode="fixed"

app:tabSelectedTextColor="@color/colorPrimary"

app:tabGravity="fill">

上述代码中已经声明了TabLayout的常见属性,解释如下:

app:tabIndicatorHeight:指定选项卡选中时的指示器高度,通常与app:tabIndicatorColor一起使用以设置选项卡选中样式,当需要自定义选中效果时,可将高度设置为0;

app:tabTextAppearance:指明选项卡中的文字样式,TabLayout默认选项卡中的所有英文均启用大写模式,如需关闭,需要在自定义的Style中添加false;

app:tabSelectedTextColor:设置选项卡选中时的字体颜色,与之对应的是app:tabTextColor;

app:tabGravity:指定选项卡的对其方式,有fill和center两个备选值,其中fill使得所有选项卡等分排布,填充整个父控件的宽度,center使得选项卡按照固定大小排列,超出最大宽度后可滚动(个数不够填满最大宽度时会居中显示,平板上见过);

app:tabMode:选项卡的工作模式,有fixed和scrollable两个备选值,其中scrollable通常与ViewPager配合使用;

创建TabItem

我们可以通过两种方式创建TabItem,一种是使用TabLayout的API创建,另一种是在XML中使用TabItem创建.

XML

在XML中创建代码如下:

android:id="@+id/tab_layout"

android:layout_width="match_parent"

android:layout_height="wrap_content">

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:text="Tab 1"

android:icon="@mipmap/ic_launcher" />

甚至说我们可以在这里使用自定义布局,代码如下:

android:id="@+id/tab_layout"

android:layout_width="match_parent"

android:layout_height="wrap_content">

android:layout="@layout/tab_item"

android:text="Tab 1"

android:icon="@drawable/ic_launcher" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:gravity="center_vertical">

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

需要注意的是这里的TextView和ImageView 的id必须使用系统的id,如上所示,系统的TabItem使用CustomView时调用的id为icon和text1,TabItem中的代码如下:

//TabLayout.java#1664 lines

if (custom != null) {

......

mCustomTextView = (TextView) custom.findViewById(android.R.id.text1);

if (mCustomTextView != null) {

mDefaultMaxLines = TextViewCompat.getMaxLines(mCustomTextView);

}

mCustomIconView = (ImageView) custom.findViewById(android.R.id.icon);

}

Java

使用Java代码,我们只需要使用TabLayout.newTab()创建一个Tab对象,然后使用TabLayout.addTab(Tab tab)添加即可。代码如下:

mTabLayout.addTab(mTabLayout.newTab().setText("first Tab").setIcon());

记得根据需求设置Tab的常见属性,例如说文本和图片等

示例

下面我们根据前面描述编写一个简单的Tab页面,实现后效果如下:

241a13a541795193c0e39d9f57463b5c.png

其中第一个选项卡来源于XML,剩余四个选项卡来源于Java代码。

首先添加TabLayout到布局文件中,并设置TabLayout在最底部,代码如下:

android:id="@+id/tabs"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:minHeight="?attr/actionBarSize"

app:tabTextAppearance="@style/MyCustomTextAppearance"

app:tabIndicatorHeight="0dp"

app:tabMode="scrollable"

app:tabSelectedTextColor="@color/colorPrimary"

app:tabGravity="center"

>

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:text="Tab 1"

android:icon="@mipmap/ic_launcher" />

在上述代码中我已经使用XML初始化了一个基本的TabItem。随后在对应的Activity找到该TabLayout,动态添加Tab,代码如下:

mTabLayout = findViewById(R.id.tabs);

//添加Tab

mTabLayout.addTab(mTabLayout.newTab().setText("first Tab").setIcon(R.drawable.bottom_bar_screen_record_state_list));

mTabLayout.addTab(mTabLayout.newTab().setText("second Tab").setIcon(R.drawable.bottom_bar_video_manager_state_list));

mTabLayout.addTab(mTabLayout.newTab().setText("third Tab").setIcon(R.drawable.bottom_bar_discover_state_list));

mTabLayout.addTab(mTabLayout.newTab().setText("four Tab").setIcon(R.drawable.bottom_bar_match_state_list));

运行后即可得到开始的效果图,这里如果我们修改app:tabMode="scrollable",就会得到滚动效果,如下所示:

9d257d2f78f15eeba8ee448c1e7a92aa.gif

完整代码参见:

https://github.com/tuozhaobing/CsdnDemoCode/tree/master/TabLayoutDemo

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android TabLayout是一个用于显示标签页的控件,可以在布局中使用TabLayout来创建并管理多个标签页。可以使用XML布局文件来定义TabLayout,并设置其属性来自定义样式和行为。 在XML布局中,可以使用TabLayout标签来创建TabLayout控件,并使用TabItem标签来创建每个标签页的布局。可以设置TabItem的属性来定义每个标签页的文本、图标和样式。 如果标签页数量较多且超出屏幕宽度,可以使用属性tabMode="scrollable"来设置TabLayout可滚动,以便所有标签页都可以显示在屏幕上。 以下是一个示例的XML布局代码: ``` <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <com.google.android.material.tabs.TabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:tabTextColor="@color/colorPrimary" app:tabSelectedTextColor="@color/colorPrimaryDark" /> <androidx.viewpager.widget.ViewPager android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintTop_toBottomOf="@id/tab_layout" /> </androidx.constraintlayout.widget.ConstraintLayout> ``` 这段代码定义了一个包含TabLayout和ViewPager的ConstraintLayout布局。TabLayout用于显示标签页,ViewPager用于显示与标签页对应的内容页。你可以根据自己的需要修改这个布局,并在代码中使用TabLayout和ViewPager来实现TabLayout的功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Android控件-TabLayout使用介绍](https://blog.csdn.net/csdnxia/article/details/105947804)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Android控件——Tablayout使用浅析(一)](https://blog.csdn.net/weixin_43499030/article/details/90179867)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值