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 ...
随机推荐
- Python 隔离环境 virtualenv
1) 安装 $ sudo pip3 install virtualenv 2) 创建并进入工程目录,例如 myproject $ mkdir myproject $ cd myproject 3) 在 ...
- 基础数据类型:整型int、布尔值bool、字符串str、与for循环
1.整型 int() p2 long 长整型 p3 全部都是整型 2.布尔值 bool() True --- int() int(True) int() --- True bool(int) 注意点: ...
- keras 的 Deeplabv3+ 实现遇到的问题
代码大佬都已经写好了,具体参考:https://github.com/bonlime/keras-deeplab-v3-plus git clone 下来以后,按照指南要训练自己的数据集,只要设置好自 ...
- 基于 HTML5 WebGL 的 3D 工控裙房系统
前言 工业物联网在中国的发展如火如荼,网络基础设施建设,以及工业升级的迫切需要都为工业物联网发展提供了很大的机遇.中国工业物联网企业目前呈现两种发展形式并存状况:一方面是大型通讯.IT企业的布局:一方 ...
- 一篇博客带你入门Flask
一. Python 现阶段三大主流Web框架 Django Tornado Flask 对比 1.Django 主要特点是大而全,集成了很多组件,例如: Models Admin Form 等等, 不 ...
- Reachability from the Capital CodeForces - 999E (强连通)
There are nn cities and mm roads in Berland. Each road connects a pair of cities. The roads in Berla ...
- [转帖]Go中的下划线
Go中的下划线 https://blog.csdn.net/wanglei9876/article/details/50475864 下划线的作用: 在import 时 是仅引入 init 函数 在正 ...
- HttpInvoker http请求工具类
import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import j ...
- Python——Flash框架——用户认证
一.认证扩展 1.Flask-Login:管理已登录用户的用户会话 2.Werkzeug:计算几码散列值并进行核对 3.itsdangerous:生成并核对加密安全令牌 二.Werkzeug gene ...
- [模板] 杜教筛 && bzoj3944-Sum
杜教筛 浅谈一类积性函数的前缀和 - skywalkert's space - CSDN博客 杜教筛可以在\(O(n^{\frac 23})\)的时间复杂度内利用卷积求出一些积性函数的前缀和. 算法 ...