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 ...
随机推荐
- flutter 按钮单选封装
数字是自己先写死的 List list =[ { "title": "影视特效", , }, { "title": "室内设计&q ...
- DLL注入技术之劫持进程创建注入
劫持进程创建注入原理是利用Windows系统中CreateProcess()这个API创建一个进程,并将第6个参数设为CREATE_SUSPENDED,进而创建一个挂起状态的进程,利用这个进程状态进行 ...
- linux下df查看空间已经占用%100,但是找不到大文件的解决方法
有时候在linux下会遇到这种情况:df查看空间已经占用%100,但是找不到大文件,怎么回事呢,经过网上查找资料,得到解决方法: 1.使用lsof查看已删除但未释放的文件 lsof -n | grep ...
- class13and14and15_登录窗口
最终的运行效果图(程序见序号6.2): #!/usr/bin/env python# -*- coding:utf-8 -*-# ----------------------------------- ...
- tensorflow 训练的时候loss=nan
出现loss为nan 可能是使用了relu激活函数,导致的.因为在负半轴上输出都是0
- USACO2012 Haybale stacking /// 区间表示法 oj21556
题目大意:N个方块 标号1~N K个操作 操作a b 表示标号a~b区间每位多加一个方块 Input * Line 1: Two space-separated integers, N K. * ...
- 使用PyCharm创建Django项目及基本配置
https://segmentfault.com/a/1190000011576316 pycharm是个很不错的python开发工具,大大缩短了python项目的创建时间以及调试时间在使用pytho ...
- String 字符串对象
String是什么 String字符串,是一种引用数据类型,并不是基础数据类型. 对于基础数据类型和引用数据类型的区别: 基础数据类型,在创建时直接将值存放在栈内存中. 引用数据类型,在创建时栈内存中 ...
- iOS开发系列-Shell脚本生成IPA
概述 在公司开发到了测试阶段需要频繁打包交付给测试,看似简单的工作,重复的流程总是感觉不是那么好,我们可以借助苹果提供的编译指令编译项目. 自动化脚本编译打包IPA 常见的iOS项目就是基于xcode ...
- laravel 优化小记
laravel 优化 7 Performance Optimization Tips for the Laravel Developer 运行 php artisan optimize php art ...