Android开发 Tablayout的学习
前言
Tablayout一般做主页底下的导航栏开发或者上面的选择栏开发,就个人感觉Tablayout用于主页导航栏会比BottomNavigationView更好,自定义方面也更容易.缺点是没有动画也不是Material Design设计模式的一部分.下面就讲解用于有导航栏的主页开发:
一般主页导航栏与内容用Tablayout与Fragment配合使用
1.第一种Tablayout+ViewPager+Fragment,好处是可以左右滑动不需要自己实现滑动,并且可以有动画出现
2.第二种Tablayout+Fragment,如果你不需要左右滑动就可以轻松的选择这个模式.
在xml里
因为有2种添加Item的方式,所以写法也可以是2个种
第一种,这种方法可以直接配置Item的名称属性,注意!这种方法还可以设置图标android:icon=
<com.google.android.material.tabs.TabLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"> <com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </com.google.android.material.tabs.TabLayout>
第二种,这种需要自己在代码里添加子Item,并且可以实现自定义布局和View的Item(下面会说明在代码里添加Item)
<com.google.android.material.tabs.TabLayout
android:id="@+id/tablayout"
android:layout_width="0dp"
android:layout_height="56dp"
app:tabIndicatorHeight="0dp"
app:tabBackground="@android:color/transparent"
app:tabRippleColor="@android:color/transparent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
</com.google.android.material.tabs.TabLayout>
代码部分讲解
方式1.如果你需要与ViewPager配合使用,那么你需要知道下面的关键代码
ContentFragment fragment1 = new ContentFragment();
ContentFragment fragment2 = new ContentFragment();
ContentFragment fragment3 = new ContentFragment();
ContentFragment fragment4 = new ContentFragment();
fragments.add(fragment1);
fragments.add(fragment2);
fragments.add(fragment3);
fragments.add(fragment4);
adapter = new FragmentAdapter(getSupportFragmentManager(), fragments);
viewpager.setAdapter(adapter);
tablayout.setupWithViewPager(viewpager);
tablayout.setupWithViewPager(viewpager);是关键,负责连接ViewPager与Tablayout,当然选择的适配器FragmentAdapter也是关键。
也可以使用FragmentStatePagerAdapter与FragmentPagerAdapter 这2个适配器, FragmentStatePagerAdapter适合在页面很多的情况下使用因为它会清理一些已经滑动走的Fragment, 而FragmentPagerAdapter会保存所有Fragment
方式2.如果你是需要自定义Item(下面用创建Tab的方式添加到TabLayout里,就前面说的代码里添加Item)
private void addTabData() {
mTablayout = findViewById(R.id.tablayout);
String[] tabContentArray = {"首页", "通知", "圈子", "我的"};
int[] tabIconArray = {R.drawable.ic_home_homepage, R.drawable.ic_home_notice, R.drawable.ic_home_circle, R.drawable.ic_home_my};
for (int i = 0; i < 4; i++) {
TabLayout.Tab tab = mTablayout.newTab();//关键的创建一个Tab,注意这里使用的是已经实例的mTablayout创建的Tab,很容易疏忽使用new Tablayout().new Tab()的方式创建,这个是会报错的.
View view = LayoutInflater.from(this).inflate(R.layout.home_tab_item,mTablayout,false);
ImageView icon = view.findViewById(R.id.icon);
TextView content = view.findViewById(R.id.content);
icon.setImageResource(tabIconArray[i]);
content.setText(tabContentArray[i]);
tab.setCustomView(view);
if (i == 0){
mTablayout.addTab(tab,0,true);//设置选择的item
}else {
mTablayout.addTab(tab);
}
}
}
home_tab_item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="35dp"
android:layout_height="wrap_content"> <TextView
android:id="@+id/red_dot"
android:layout_width="10dp"
android:layout_height="10dp"
android:background="@drawable/bg_reddot"
app:layout_constraintTop_toTopOf="@id/icon"
app:layout_constraintLeft_toRightOf="@id/icon"
app:layout_constraintRight_toRightOf="@id/icon"/> <ImageView
android:id="@+id/icon"
android:layout_width="24dp"
android:layout_height="24dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/> <TextView
android:id="@+id/content"
android:textColor="@color/fontColorMain2"
android:textSize="@dimen/font_size_11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
app:layout_constraintTop_toBottomOf="@id/icon"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/> </androidx.constraintlayout.widget.ConstraintLayout>
ic_home_homepage.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@mipmap/ic_home_page_select"/>
<item android:state_selected="false" android:drawable="@mipmap/ic_home_page"/> </selector>
实现图片的选中变化
Tablayout选中回调
private void initTablayoutListener(){
mTablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
//选中某个tab
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
//当tab从选择到未选择
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
//已经选中tab后的重复点击tab
}
});
}
XML属性讲解
设置Tab的TextSize
首先你需要在styles.xml文件里创建这个文字属性的styles
<style name="TabLayoutTextStyle">
<item name="android:textSize">@dimen/font_size_16</item>
</style>
然后在使用以下属性导入styles
app:tabTextAppearance="@style/TabLayoutTextStyle"
如果你不需要选中下划线,可以使用这个属性取消
app:tabIndicatorHeight="0dp"
如果你不需要点击后的阴影加涟漪动画效果,可以使用下面2个属性取消
app:tabBackground="@android:color/transparent"
app:tabRippleColor="@android:color/transparent"
app:tabIndicatorColor :指示线的颜色
app:tabIndicatorHeight : 指示线的高度
app:tabIndicatorFullWidth="false" :指示线是否铺满整个tab宽度,可以配合实现圆角
app:tabSelectedTextColor : tab选中时的字体颜色
app:tabTextColor="@color/colorPrimary" :未选中字体颜色
app:tabBackground="color" : 整个tab的背景颜色
app:tabMode="scrollable" : 默认是fixed,固定的;scrollable:可滚动的
app:tabMaxWidth="0dp" :tab的最大宽度
app:tabMinWidth="0dp" :tab的最小宽度
app:tabPaddingTop="0dp" :tab上内边距
app:tabPadding="0dp" :tab的内边距
app:tabGravity="center" :tab的位置
app:tabIndicator= : 自定义指示线,注意!这里的指示线其实是android:state_selected="true"的选中状态,所以如果单单写一个shape的xml文件设置是无法显示的,需要在写一个选中状态的xml。参考如下:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="8dp"
android:left="8dp"
android:right="8dp"
android:top="8dp"> <!-- 设置边距 -->
<shape> <!-- 设置圆角 -->
<corners android:radius="5dp" /> <!-- 设置边框 -->
<stroke
android:width="1dp"
android:color="@color/colorAccent" />
</shape>
</item>
</layer-list>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/indicator" android:state_selected="true" />
</selector>
app:tabTextAppearance="@style/tab_layout_item_text" : 改变字体大小颜色等等,style参考如下:
<style name="tab_layout_item_text">
<item name="android:textSize">16sp</item>
<item name="android:textColor">@color/color_18</item>
</style>
以下是从View的源码里复制来的属性,以下属性仅做参考,有些并没有提供方法设置.比如tabTextSize 有归有,但是人家不提供方法设置..除非自己继承重写
int tabPaddingStart;
int tabPaddingTop;
int tabPaddingEnd;
int tabPaddingBottom;
int tabTextAppearance;
android.content.res.ColorStateList tabTextColors;
android.content.res.ColorStateList tabIconTint;
android.content.res.ColorStateList tabRippleColorStateList;
@androidx.annotation.Nullable
android.graphics.drawable.Drawable tabSelectedIndicator;
android.graphics.PorterDuff.Mode tabIconTintMode;
float tabTextSize;
float tabTextMultiLineSize;
final int tabBackgroundResId;
int tabMaxWidth;
private final int requestedTabMinWidth;
private final int requestedTabMaxWidth;
private final int scrollableTabMinWidth;
private int contentInsetStart;
int tabGravity;
int tabIndicatorAnimationDuration;
int tabIndicatorGravity;
int mode;
boolean inlineLabel;
boolean tabIndicatorFullWidth;
boolean unboundedRipple;
补充内容
在代码里选中某一个TabItem
mTabLayout.getTabAt(0).select();
你从select()方法就可以看出.就是希望你使用select()方法来切换item
end
app:tabPaddingTop="0dp"
app:tabPaddingStart="0dp"
Android开发 Tablayout的学习的更多相关文章
- Android开发系列之学习路线图
通过前面的3篇博客已经简单的介绍了Android开发的过程并写了一个简单的demo,了解了Android开发的环境以及一些背景知识. 接下来这篇博客不打算继续学习Android开发的细节,先停一下,明 ...
- Android开发—— Tablayout的使用
Tablayout的使用 属性 属性名 说明 app:tabMod 设置Tab模式 app:tabTextColor 设置文本颜色 app:tabSelectedTextColor 设置选中文本颜色 ...
- android开发艺术探索学习 之 结合Activity的生命周期了解Activity的LaunchMode
转载请标明出处: http://blog.csdn.net/lxk_1993/article/details/50749728 本文出自:[lxk_1993的博客]: 首先还是先介绍下Activity ...
- Android开发艺术探索学习笔记(三)
第三章 View的事件体系 3.1 View基础知识 3.1.1 什么是view View 是Android中所有控件的基类,是一种界面层的控件的一种抽象,它代表了一个控件. 3.1.2 View的 ...
- Android开发艺术探索学习笔记(十一)
第十一章 Android的线程和线程池 从用途上来说,线程分为子线程和主线程,主线程主要处理和界面相关的事情,而子线程往往用于执行耗时的操作.AsyncTask,IntentService,Hand ...
- Android开发艺术探索学习笔记(十)
第十章 Android的消息机制 面试中经常会被问到的一个问题:handler是如何在子线程和主线程中进行消息的传递的,这个问题通过了解Android的消息机制可以得到一个准确的答案. Androi ...
- Android开发艺术探索学习笔记(六)
第六章 Android的Drawable Drawable的优点:使用简单,比自定义view的成本要低:非图片类型的Drawable占用空间小,有利于减小APK安装包的大小. 6.1Drawable ...
- Android开发艺术探索学习笔记(四)
第四章 View的工作原理 4.1初识ViewRoot和DecorView ViewRoot是连接WindowManager和DecorView的纽带,View的三大流程均是通过ViewRoot来完成 ...
- Android开发艺术探索学习笔记(一)
第一章 Activity的生命周期和启动模式 1.1Activity的生命周期全面解析 1.1.1典型情况下的生命周期分析 (1)在两个Activity进行切换时,当前的Activity的onPaus ...
随机推荐
- NX二次开发-NXOPEN获取所有工程图和所有视图DrawingSheet,DrawingSheetCollection,DraftingView
NX11+VS2013 #include <NXOpen/Part.hxx> #include <NXOpen/PartCollection.hxx> #include < ...
- 剑指offer——13矩阵中的路径
题目描述 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径.路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子.如果一条路径经过了矩阵中 ...
- Victor and String HDU - 5421 双向回文树
题意: 有n种操作,开始给你一个空串,给你4中操作 1 c 在字符串的首部添加字符c 2 c 在字符串的尾部添加字符c 3 询问字符中的本质不同的回文串的个数 4 询问字符串中回文串的个数 思路 ...
- javascript面向对象编程笔记(函数)
第三章 函数 3.1 什么是函数 一般来说,函数声明通常由以下几部分组成: function子句 函数名称 函数所需参数 函数体 return子句.如果某个函数没有显示的返回值,默认它的返回值为und ...
- iOS开发系列-Shell脚本编译SDK
Library静态库Shell脚本 #!/bin/bash #要build的target名 target_Name="IFlyMSC" #编译模式 Release.Debug bu ...
- vue cli3使用webpack4打包优化
去掉console.log,以及开启gzip const CompressionPlugin = require('compression-webpack-plugin');//引入gzip压缩插件 ...
- 2019 年百度之星·程序设计大赛 - 初赛一 C. Mindis 离散化+dijkstra
题目传送门 题意:中文题面 思路: 先将所有题目给出的点离散化一下,得到一张n*m的网格,n和m最大都是400,所以我们只需要枚举每个加强的区域,将属于这个区域的边处理一下(所有横着的和竖着的边,暴力 ...
- Laravel 迁移检查表是否存在
Schema::hasTable('TableName'); //检查表释放存在 Schema::hasColumn('tableName', 'columeName'); //检查表是否存在某个字段 ...
- [JZOJ3362] 【NOI2013模拟】数数
题目 题目大意 求区间\([A,B]\)有多少个数是"完美的". 一个数是"完美的",当且仅当这个数的各位能分成两个集合,使得两个集合中数字的和相等. \(B\ ...
- react 实现类似vue中的<keep-alive>的功能,并解决antd-mobile切换回来时的空白
在移动端的spa页面中,只要使用到了路由就很有必要使用到状态保存的功能,这样才能保证在页面进行切换的时候,让用户可以看到刚才滑动的地方,让用户的体验更加友好.这儿我找到了react-router-ca ...