实现 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设置选中标签字体加粗功能的更多相关文章

  1. css点击li里面的标签 点击当前标签字体加粗 之前的恢复原始状态

    <div class="functionalNavigation"> <ul class="ulp"> <icon class=& ...

  2. android textview字体加粗 Android studio最新水平居中和垂直居中

    android textview字体加粗 Android studio最新水平居中和垂直居中 Android中字体加粗在xml文件中使用android:textStyle=”bold”但是不能将中文设 ...

  3. html小知识点汇总(浏览器导航上显示图标、div无高度时试着清除浮动、文字环绕图片、字体加粗、div按百分比分、已有的不合适的class,针对特定的标签进行修改)

    1.新点击的网页,在浏览器导航上显示图标: 像这种效果: <head> <meta charset="UTF-8"> <meta name=" ...

  4. html字体加粗标签与写法

    在html中字体加粗的标签为<b>标签,当我们使用了该标签,字体就会加粗,一般用于注明重要信息,强调文字上面写法如下 字体加粗:<b>这里的字体就会加粗</b> 效 ...

  5. richTextBox设置选中的字体属性

      执行一次设置选中的字体样式 再执行一次恢复正常     //粗体 public void ToggleBold() { if (richTextBox1.SelectionFont == null ...

  6. HTML怎么实现字体加粗

    HTML的加粗标签是<b>标签,是用来对你自定文字加粗,写法如下: 字体加粗:<b>这里是加粗的内容</b> 这样就可以实现加粗了!

  7. C# DataGrid根据某列的内容设置行字体加粗 单元格设置对齐方式

    最近做了个功能,DataGrid显示具体内容的时候,根据某列分组. 每个分组具体内容后边,增加一行显示合计信息. 查询数据时,使用了union all将分组数据与明细数据合并起来,使用了排序达到了预期 ...

  8. Android TextView中 字体加粗方法

    textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));//加粗 textView.getPaint().setFakeBoldT ...

  9. UILabel字体加粗等属性和特效

    /* Accessing the Text Attributes text  property font  property textColor  property textAlignment  pr ...

随机推荐

  1. Java list 转字符串并加入分隔符的方法

    import org.apache.commons.lang.StringUtils; List<String> list=new ArrayList<String>(); l ...

  2. 012_call和apply区别

    一. function fn(a,b) { console.log(this); } fn.call(null,1,2); //call为参数方式 fn.apply(null,[1,2]); //ap ...

  3. mongodb安装4.0(rpm)

    虚拟机客户端vmware player linux版本:CentOS Linux release 7.4.1708 (Core) CentOS安装类型:Basic Web Server 参照官网最新文 ...

  4. 聊聊计算机中的编码(Unicode,GBK,ASCII,utf8,utf16,ISO8859-1等)以及乱码问题的解决办法

    作为一个程序员,一个中国的程序员,想来“乱码”问题基本上都遇到过,也为之头疼过.出现乱码问题的根本原因是编码与解码使用了不同而且不兼容的“标准”,在国内一般出现在中文的编解码过程中. 我们平时常见的编 ...

  5. git完全cli指南之详细思维导图整理分享

    一直以来都觉得 在开发过程中 能用命令行的还是用命令行 能用快捷键的就要快捷键 前期可能要点学习成本 但是熟练后带来的好处还是非常可观的 所以一直坚持使用命令行的方式来使用git 基本上每个操作都能心 ...

  6. 从 0 到 1 实现 React 系列 —— 1.JSX 和 Virtual DOM

    看源码一个痛处是会陷进理不顺主干的困局中,本系列文章在实现一个 (x)react 的同时理顺 React 框架的主干内容(JSX/虚拟DOM/组件/生命周期/diff算法/setState/ref/. ...

  7. Golang常见误区(二)

    35. 关闭 HTTP 的响应体 使用 HTTP 标准库发起请求.获取响应时,即使你不从响应中读取任何数据或响应为空,都需要手动关闭响应体.新手很容易忘记手动关闭,或者写在了错误的位置: // 请求失 ...

  8. js模块化世界

    前言 我们经常见到 一些这样的写法,require('xxx').import xx from '../components/data'.export const data....也听见一些这样的说法 ...

  9. 安装appium桌面版和命令行版

    一 桌面版(打开很慢,常用于辅助元素定位) 1.官网下载window版本: 2.直接点击紫色图标即可打开   3.启动server 二  命令行版(打开很快,常用于执行脚本) 1.jdk 安装jdk并 ...

  10. python 必学模块collections

    包含的主要功能如下 查看collections 的源码我们可以看到其为我们封装了以下的数据结果供我们调用 __all__ = ['deque', 'defaultdict', 'namedtuple' ...