android TabLayout设置选中标签字体加粗功能

本文介绍如何通过XML配置及Java代码实现TabLayout中选中Tab标签时字体加粗的效果。具体步骤包括定义样式、创建Tab并设置自定义视图等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现 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`的选中标签添加点击样式加粗,你可以通过自定义一个`TextView`样式,并将其应用到你的`TabLayout`的`TabGravity`上。以下是步骤: 1. 首先,在你的项目资源文件`styles.xml`中创建一个新的样式,例如,名为`BoldTextAppearance`,并将`android:textStyle`属性设置为`bold`,表示加粗文本: ```xml <style name="BoldTextAppearance" parent="Base.TextAppearance.AppCompat.Widget.ActionBar.Title"> <item name="android:textSize">16sp</item> <!-- 自定义字体大小 --> <item name="android:textColor">@color/tab_selected_color</item> <!-- 设置选中颜色 --> <item name="android:textStyle">bold</item> </style> ``` 确保将`android:textSize`调整为你想要的字体大小。 2. 然后,在你的`Activity`或`Fragment`中初始化`TabLayout`时,为其设置这个自定义样式作为默认项: ```java TabLayout tabLayout = findViewById(R.id.tab_layout); tabLayout.getTabTextColors().withDefaultColorStateList(ContextCompat.getColorStateList(this, R.color.tab_text_color)); tabLayout.setTabTextAppearance(R.style.BoldTextAppearance); ``` 这里假设你已经有了用于未选中状态的颜色(`tab_text_color`),以及选中状态的颜色(`tab_selected_color`)。 3. 最后,如果你还想在用户点击时保持加粗效果,可以监听`TabLayout`的`onTabSelected()`事件并更新相应的`TextView`样式: ```java tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { if (tab != null) { TextView textView = (TextView) tab.getCustomView(); if (textView != null) { textView.setTextAppearance(tab.getContext(), R.style.BoldTextAppearance); } } } // 其他方法... }); ``` 现在,当`TabLayout`中的标签被点击时,它们将以加粗的样式显示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值