android TabLayout设置选中标签字体加粗功能
实现 TabLayout 选中tab标签字体加粗功能如下:
xml文件中定义:
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
app:tabIndicatorColor="@color/orange"
app:tabIndicatorHeight="2dp"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/_333333"
app:tabTextColor="@color/_4c4c4c"
app:tabTextAppearance="@style/TabLayoutStyle"/>
<style name="TabLayoutStyle">
<item name="android:textSize">18sp</item>
<item name="android:textStyle">bold</item>
</style>
java文件中设置:
//引用tablayout
ArrayList<String> tabList = new ArrayList<>();
tabList.add("tab1");
tabList.add("tab2");
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText(tabList.get(0));
tabLayout.addTab(tabLayout.newTab().setText(tabList.get(1))); for (int i = 0; i < tabLayout.getTabCount(); i++) {
TabLayout.Tab tab = tabLayout.getTabAt(i);
if (tab != null) {
tab.setCustomView(getTabView(i));
}
}
updateTabTextView(tabLayout.getTabAt(tabLayout.getSelectedTabPosition()), true); tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
updateTabView(tab, true); } @Override
public void onTabUnselected(TabLayout.Tab tab) {
updateTabView(tab, false);
} @Override
public void onTabReselected(TabLayout.Tab tab) { }
});
} private View getTabView(int currentPosition) {
View view = LayoutInflater.from(this).inflate(R.layout.tab_item, null);
TextView textView = (TextView) view.findViewById(R.id.tab_item_textview);
textView.setText(tabList.get(currentPosition));
return view;
} private void updateTabTextView(TabLayout.Tab tab, boolean isSelect) { if (isSelect) {
//选中加粗
TextView tabSelect = (TextView) tab.getCustomView().findViewById(R.id.tab_item_textview);
tabSelect.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
tabSelect.setText(tab.getText());
} else {
TextView tabUnSelect = (TextView) tab.getCustomView().findViewById(R.id.tab_item_textview);
tabUnSelect.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
tabUnSelect.setText(tab.getText());
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" > <TextView
android:id="@+id/tab_item_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_gravity="center"
android:gravity="center"
android:textColor="@color/333333"
/> </LinearLayout>
android TabLayout设置选中标签字体加粗功能的更多相关文章
- css点击li里面的标签 点击当前标签字体加粗 之前的恢复原始状态
<div class="functionalNavigation"> <ul class="ulp"> <icon class=& ...
- android textview字体加粗 Android studio最新水平居中和垂直居中
android textview字体加粗 Android studio最新水平居中和垂直居中 Android中字体加粗在xml文件中使用android:textStyle=”bold”但是不能将中文设 ...
- html小知识点汇总(浏览器导航上显示图标、div无高度时试着清除浮动、文字环绕图片、字体加粗、div按百分比分、已有的不合适的class,针对特定的标签进行修改)
1.新点击的网页,在浏览器导航上显示图标: 像这种效果: <head> <meta charset="UTF-8"> <meta name=" ...
- html字体加粗标签与写法
在html中字体加粗的标签为<b>标签,当我们使用了该标签,字体就会加粗,一般用于注明重要信息,强调文字上面写法如下 字体加粗:<b>这里的字体就会加粗</b> 效 ...
- richTextBox设置选中的字体属性
执行一次设置选中的字体样式 再执行一次恢复正常 //粗体 public void ToggleBold() { if (richTextBox1.SelectionFont == null ...
- HTML怎么实现字体加粗
HTML的加粗标签是<b>标签,是用来对你自定文字加粗,写法如下: 字体加粗:<b>这里是加粗的内容</b> 这样就可以实现加粗了!
- C# DataGrid根据某列的内容设置行字体加粗 单元格设置对齐方式
最近做了个功能,DataGrid显示具体内容的时候,根据某列分组. 每个分组具体内容后边,增加一行显示合计信息. 查询数据时,使用了union all将分组数据与明细数据合并起来,使用了排序达到了预期 ...
- Android TextView中 字体加粗方法
textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));//加粗 textView.getPaint().setFakeBoldT ...
- UILabel字体加粗等属性和特效
/* Accessing the Text Attributes text property font property textColor property textAlignment pr ...
随机推荐
- ubuntu添加普通用户,并解决远程登录
创建普通用户 # 创建用户,并指定用户目录,加入用户组sudo useradd username -d /home/username -m #设置密码 sudo passwd username #给用 ...
- 模型加速[tensorflow&tensorrt]
在tensorflow1.8之后的版本中,tensorflow.contrib部分都有tensorrt的组件,该组件存在的意义在于,你可以读取pb文件,并调用tensorrt的方法进行subgraph ...
- 从PyMongo看MongoDB Read Preference
在CAP理论与MongoDB一致性.可用性的一些思考一文中提到,MongoDB提供了一些选项,如Read Preference.Read Concern.Write Concern,对MongoD ...
- PyInstaller安装使用方法
PyInstaller可以把Python应用程序及其所有依赖项捆绑到一个包中.用户可以在不安装Python解释器或任何模块的情况下运行打包的应用程序.PyInstaller支持Python 2.7和P ...
- 聊聊计算机中的编码(Unicode,GBK,ASCII,utf8,utf16,ISO8859-1等)以及乱码问题的解决办法
作为一个程序员,一个中国的程序员,想来“乱码”问题基本上都遇到过,也为之头疼过.出现乱码问题的根本原因是编码与解码使用了不同而且不兼容的“标准”,在国内一般出现在中文的编解码过程中. 我们平时常见的编 ...
- 在 CentOS 7 中安装 MySQL 8
准备 本文环境信息: 软件 版本 CentOS CentOS 7.4 MySQL 8.0.x 安装前先更新系统所有包 sudo yum update 安装 1. 添加 Yum 包 wget https ...
- AOP从静态代理到动态代理 Emit实现
[前言] AOP为Aspect Oriented Programming的缩写,意思是面向切面编程的技术. 何为切面? 一个和业务没有任何耦合相关的代码段,诸如:调用日志,发送邮件,甚至路由分发.一切 ...
- すぬけ君の塗り絵 / Snuke's Coloring AtCoder - 2068 (思维,排序,贡献)
Problem Statement We have a grid with H rows and W columns. At first, all cells were painted white. ...
- Jmeter二次开发代码(1)
package org.apache.jmeter.functions; import java.util.Collection;import java.util.LinkedList;import ...
- k8s简单的来部署一下tomcat,并测试自愈功能
前言: 2018年12月6日 今天终于把k8s运行tomcat打通了,耗了我几天时间一个一个坑踩过来,不容易啊,废话不多说. 先记录一些操作时的错误: <<<<<< ...