android ------ AndroidX的 Tablayout(com.google.android.material.tabs.TabLayout) 的使用
前面呢,有写过TabLayout的博客,最近开发用到了AndroidX来解决前面的问题,不要工具类设置下划线的问题了,来总结一下
Android--------TabLayout实现新闻客户端顶部导航栏
Android中Tablayout设置下划线宽度 和 dp和px之间进行相互转换
AndroidX效果图

首先添加依赖:
以前的是
implementation 'com.android.support:design:28.0.0'
换成
implementation "com.google.android.material:material:1.0.0"
现在的TabLayout有2种添加Item的方式
第一种和以前的差不多
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMaxWidth="0dp"
app:tabIndicatorColor="@color/colorAccent"
app:tabSelectedTextColor="@color/colorPrimary"
app:tabTextColor="@color/colorPrimary"
app:tabIndicatorFullWidth="false"
app:tabBackground="@color/transparent"
app:tabRippleColor="@color/transparent"
> </com.google.android.material.tabs.TabLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#E4E4E4"
></View> <androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" > </androidx.viewpager.widget.ViewPager>
Java代码
public class MainActivityB extends AppCompatActivity {
static final int NUM_ITEMS = 4;
private List<Fragment> fragmentList = new ArrayList<Fragment>();
private String[] strings = new String[]{"A","B","C","D"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainb);
fragmentList.add(new FragmentA());
fragmentList.add(new FragmentB());
fragmentList.add(new FragmentC());
fragmentList.add(new FragmentD());
initView();
}
private void initView(){
TabLayout tab_layout = findViewById(R.id.tab_layout);
ViewPager viewPager = findViewById(R.id.viewPager);
MyAdapter fragmentAdater = new MyAdapter(getSupportFragmentManager());
viewPager.setAdapter(fragmentAdater);
tab_layout.setupWithViewPager(viewPager);
}
public class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return NUM_ITEMS;
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return strings[position];
}
}
}
如果你不需要点击后的阴影动画效果,可以使用下面2个属性取消
app:tabBackground="@android:color/transparent"
app:tabRippleColor="@android:color/transparent"
看布局里面有一个这样的属性
tabIndicatorFullWidth (boolean)
默认为true ,是否使选择指示器的宽度适合选项卡项目的整个宽度(意思就是将选择指示器宽度将设置为最小宽度值,就是字体的宽度)
这样就解决了之前的问题了,
属性详细介绍请看文档:https://developer.android.google.cn/reference/com/google/android/material/tabs/TabItem
第二种写法
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMaxWidth="0dp"
app:tabIndicatorColor="@color/colorAccent"
app:tabSelectedTextColor="@color/colorPrimary"
app:tabTextColor="@color/colorPrimary"
app:tabIndicatorFullWidth="false"
> <com.google.android.material.tabs.TabItem
android:id="@+id/tabItem1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A"
/> <com.google.android.material.tabs.TabItem
android:id="@+id/tabItem2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B"
/> <com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"
/> <com.google.android.material.tabs.TabItem
android:text="D"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </com.google.android.material.tabs.TabLayout>
这种是TabLayout 增加了一个TabItem控件,直接把Item写在里面了
这种也是很方便的,需要添加一个TabLayout的选中回调
tab_layout.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
}
});
其他属性
app:tabIndicatorColor :指示线的颜色
app:tabIndicatorHeight : 指示线的高度
app:tabIndicatorFullWidth="false" 指示线是否铺满宽度
app:tabSelectedTextColor : tab选中时的字体颜色
app:tabTextColor="@color/colorPrimary" :未选中字体颜色
app:tabBackground="color" : 整个tablayout颜色
app:tabMode="scrollable" : 默认是fixed,固定的;scrollable:可滚动的
我用两种方式实现了上面效果图,有需要代码的请star 谢谢
代码地址
参考的官方详细文档地址: https://developer.android.google.cn/reference/com/google/android/material/tabs/TabLayout
android ------ AndroidX的 Tablayout(com.google.android.material.tabs.TabLayout) 的使用的更多相关文章
- Android 5.0新特性了解(一)----TabLayout
1.在2015年的google大会上,google发布了新的Android Support Material Design库,里面包含了几个新的控件,其中就有一个TabLayout,它就可以完成Tab ...
- Android开发之漫漫长途 Fragment番外篇——TabLayout+ViewPager+Fragment
该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...
- android -------- AndroidX的迁移
Google 2018 IO 大会推出了 Android新的扩展库 AndroidX,用于替换原来的 Android扩展库,将原来的android.*替换成androidx.*:只有包名和Maven工 ...
- Android M 控件:Snackbar、Toolbar、TabLayout、NavigationView
Snackbar Snackbar提供了一个介于Toast和AlertDialog之间轻量级控件,它可以很方便的提供消息的提示和动作反馈.Snackbar的使用与Toast的使用基本相同: Snack ...
- Android入门3:从Toolbar到Material Design
在Android5.0(API 21)之后,Toolbar被Google推广,逐渐走入大家视野.具体关于Actionbar和Toolbar的对比就不多啰嗦了,跟着潮流走是没错的.下面先上张简单的效果图 ...
- Android使用com.google.android.cameraview.CameraView进行拍照
import android.Manifest;import android.annotation.SuppressLint;import android.content.Context;import ...
- Google Android 6.0 权限完全解析
注:本文只针对Google原生Android系统有效, 小米魅族等手机有自己的权限机制, 可能不适用 一.运行时权限的变化及特点 新的权限机制更好的保护了用户的隐私,Google将权限分为两类,一类是 ...
- Java基础知识强化之网络编程笔记23:Android网络通信之 Volley(Google开源网络通信库)
联合网上资料学习:http://www.open-open.com/lib/view/open1451223702339.html 一.Volley的介绍 1. Volley简介 在这之前,我们在程序 ...
- Google Android官方文档进程与线程(Processes and Threads)翻译
android的多线程在开发中已经有使用过了,想再系统地学习一下,找到了android的官方文档,介绍进程与线程的介绍,试着翻译一下. 原文地址:http://developer.android.co ...
随机推荐
- AttributeError: module 'tensorflow' has no attribute 'Session'
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/qq_33440324/article/ ...
- mongodb 通过嵌入文档中的字段排序
mongodb中的全部数据: db.testInfo.find({}) .sort({_id:-1}) .limit(100) 查询结果: /* 1 createdAt:2019/10/11 下午5: ...
- workman即时推送
https://www.workerman.net/web-sender 下载源码解压后运行 start_for_win.bat 如果提示不成功,就把php路径配置到环境变量中去即可 运行后打开浏 ...
- prometheus学习系列十一: Prometheus 报警规则配置
prometheus监控系统的的报警规则是在prometheus这个组件完成配置的. prometheus支持2种类型的规则,记录规则和报警规则, 记录规则主要是为了简写报警规则和提高规则复用的, 报 ...
- 【异常】Zipkin server java.lang.IllegalArgumentException: Prometheus requires that all meters with the same name have the same set of tag keys. There is already an existing meter containing tag keys [meth
解决办法: 添加下面配置就OK #zipkin启动报错无法访问的解决方法 management.metrics.web.server.auto-time-requests: false
- xadmin引入drf-yasg生成Swagger API文档
一.安装drf-yasg: 由于django-rest-swagger已经废弃了 所以引入了drf-yasg pip install drf-yasg 安装install drf-yasg库 http ...
- 在swift项目中若要通过pod引入第三方的swift项目,必须加上use_frameworks!
因为swift没法打.a https://www.jianshu.com/p/ac629a1cb8f5
- render()--组件--纯函数
render() 当 被调用时,它会检查 和 的变化并返回以下类型之一: React 元素.通常通过 JSX 创建.例如,<div /> 会被 React 渲染为 DOM 节点,<M ...
- BigDecimal保留小数
public class test1_format { public static void main(String[] args) { BigDecimal decimal = new BigDec ...
- A1130 | 中缀表达式、查找根节点
代码: #include <stdio.h> #include <memory.h> #include <math.h> #include <string&g ...