android 自定义tabwidget,安卓 Tab 标签-用 TabLayout、TabItem 更简洁实现更强大的标签…

上一节讲的 TabHost、TabWidget、FrameLayout 实现 Tab 标签控件,只能说又臭又长,妈的,我是来开发业务软件的,我不是围着你的几个类玩过来玩过去的。

我们期望能够用更快捷的方式实现我们所需的控件,把更多精力花在业务上。那就用 TabLayout。

第一步、添加引用

在所需 module 的 build.gradle 文件中,dependencies 节添加:implementation ‘com.android.support:appcompat-v7:26.1.0’

我这里是 26.1.0 版本,你那可能不是,如果你不知道版本,可以按 Ctrl + Alt + Shift + S,打开对话框中,选择所需 module,切换到 Dependencies 节,右边加号,选择 Library Dependency。搜索一下,就知道你具体是哪个版本了。

第二步、写布局文件

android:layout_width=”match_parent”

android:layout_height=”wrap_content”>

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:text=”基础信息”/>

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:text=”学历教育”/>

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:text=”任职经历”/>

效果已经出来了,比 TabHost 简单多了。

7f6b0a456d44e2ff6d46750a1d5cbe7e.png

与 TabHost 相比,还有一个就是样式,我们明明是红色主题,TabHost 居然给我们来一个蓝色下划线。关键是要改这个下划线颜色,并不是指定一个属性就了事的,它纯粹是不嫌麻烦。

而 TabLayout 不仅贴合我们主题,我们还可以自定义颜色,且非常方便,为 TabLayout 指定属性就可以了(注意命名空间):app:tabIndicatorColor:下划线的颜色

app:tabIndicatorHeight:下划线的高度

app:tabSelectedTextColor:选中项的文字颜色

选择事件m_tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

@Override

public void onTabSelected(TabLayout.Tab tab) {

int tabIndex = m_tabLayout.getSelectedTabPosition();

}

@Override

public void onTabUnselected(TabLayout.Tab tab) {

}

@Override

public void onTabReselected(TabLayout.Tab tab) {

}

})

相关资源:iZotope Ozone VST (臭氧) V4.0.3.274 绿色汉化版.zip-制造工具类…

来源:weixin_39857513

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年4月23日
下一篇 2021年4月23日

相关推荐