Android开发—— Tablayout的使用
程序员文章站
2023-02-20 23:46:24
Tablayout的使用 属性 |属性名 | 说明| | | | |app:tabMod |设置Tab模式 | |app:tabTextColor |设置文本颜色 | |app:tabSelectedTextColor |设置选中文本颜色 | |app:tabIndicatorColor |设置下滑 ......
tablayout的使用
属性
属性名 | 说明 |
---|---|
app:tabmod | 设置tab模式 |
app:tabtextcolor | 设置文本颜色 |
app:tabselectedtextcolor | 设置选中文本颜色 |
app:tabindicatorcolor | 设置下滑条颜色 |
app:tabmaxwidth="xxdp" | 设置最大的tab宽度 |
app:tabminwidth="xxdp" | 设置最小的tab宽度 |
使用,添加选项
- 静态创建(xml文件中添加tab)
效果:
添加一个tabitem即可,之后设置相关的属性,
<android.support.design.widget.tablayout android:id="@+id/tablayout" app:tabtextcolor="@color/coloraccent" app:tabselectedtextcolor="@color/colorprimary" app:tabindicatorcolor="@color/coloraccent" app:tabmode="fixed" android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.design.widget.tabitem android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="下载" /> <android.support.design.widget.tabitem android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="下载" /> </android.support.design.widget.tablayout>
- 动态创建(使用java代码添加tab)
先是通过findviewbyid
方法找到实例,之后调用tablayout
的newtab
方法来创建tab
tablayout.tab tab1 = mtablayout.newtab(); tab1.settext("正在下载"); mtablayout.addtab(tab1,0); tab1 = mtablayout.newtab(); tab1.settext("已下载"); mtablayout.addtab(tab1,1);
不过,使用动态的话,如果不设置相关的属性,是不能达到两个选项各自占长度一半,还得给tablayout加上下列属性
app:tabmaxwidth="0dp" app:tabgravity="fill" app:tabmode="fixed"
tablayout与viewpager联用
一句代码即可搞定
tablayout.setupwithviewpager(viewpager);
有些时候可能会出现不显示文本的情况,这时候需要在 pageradapter 里面重写一个方法
string[] titles ={"tab1","tab2"}; @override public charsequence getpagetitle(int position) { return mstrings[position]; }
参考
上一篇: 关于childNodes的删除
下一篇: vue关闭代码检查eslint
推荐阅读
-
Android编程实现悬浮窗获取并显示当前内存使用量的方法
-
PhpStorm的使用教程(本地运行PHP+远程开发+快捷键)
-
Android开发中如何使用BottomTabBar实现底部导航页
-
Android WebView的使用方法及与JS 相互调用
-
Android开发之CheckBox的简单使用与监听功能示例
-
Android绘图之Paint的使用方法详解
-
Android7.0开发实现Launcher3去掉应用抽屉的方法详解
-
Android开发中计算器的sin、cos及tan值计算问题分析
-
Android bdflow数据库神器的使用
-
Android 使用SharePerference判断是否为第一次登陆的实现代码