概述
实现 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>
转载于:https://www.cnblogs.com/haihai88/p/7867046.html
最后
以上就是粗暴高山为你收集整理的android TabLayout设置选中标签字体加粗功能的全部内容,希望文章能够帮你解决android TabLayout设置选中标签字体加粗功能所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复