androidTabLayout设置选中标签字体加粗功能实现 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.wTab().(0));
tabLayout.wTab().(1)));
for (int i = 0; i < TabCount(); i++) {
TabLayout.Tab tab = TabAt(i);
if (tab != null) {
tab.setCustomView(getTabView(i));
}
}
SelectedTabPosition()), 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.(currentPosition));
return view;
}
private void updateTabTextView(TabLayout.Tab tab, boolean isSelect) {
if (isSelect) {
/
/选中加粗
TextView tabSelect = (TextView) CustomView().findViewById(R.id.tab_item_textview);            tabSelect.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
tabSelect.Text());
} else {
TextView tabUnSelect = (TextView) CustomView().findViewById(R.id.tab_item_textview);            tabUnSelect.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
textstyletabUnSelect.Text());
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="schemas.android/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>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。