android tablayout 设置字体大小

本文详细介绍如何使用Android Material Design库中的TabLayout组件,并自定义其样式,包括字体大小、样式及选中颜色等属性,实现美观且符合应用主题的标签布局。

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

<com.google.android.material.tabs.TabLayout
    android:id="@+id/tl"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/rl_top"
    app:tabMode="fixed"
    app:tabSelectedTextColor="@color/theme"
    app:tabIndicator="@color/theme"
    app:tabIndicatorHeight="1dp"
    app:tabTextAppearance="@style/TabLayoutTextStyle"
    ></com.google.android.material.tabs.TabLayout>
<style name="TabLayoutTextStyle" parent="TextAppearance.Design.Tab">
    <item name="android:textSize">16dp</item>
    <item name="android:textStyle">bold</item>
</style>

注意字体style继承TextAppearance.Design.Tab,之前百度的字体style不需要继承就可以,可能是tablayout在不同包下的原因。

要在Android TabLayout设置字体,你可以使用自定义的TabLayout.TabView来实现。以下是一种可能的实现方法: 1. 创建一个自定义的TabView布局文件,例如"custom_tab_layout.xml": ```xml <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:textColor="@color/tab_text_color" android:textSize="@dimen/tab_text_size" android:fontFamily="@font/custom_font" /> ``` 在上面的布局中,你可以通过设置"android:fontFamily"属性来指定自定义字体。 2. 在你的Activity或Fragment中,获取TabLayout的Tab对象并设置自定义的TabView布局: ```java TabLayout tabLayout = findViewById(R.id.tab_layout); for (int i = 0; i < tabLayout.getTabCount(); i++) { TabLayout.Tab tab = tabLayout.getTabAt(i); if (tab != null) { tab.setCustomView(R.layout.custom_tab_layout); TextView tabTextView = tab.getCustomView().findViewById(android.R.id.text1); tabTextView.setText(tab.getText()); } } ``` 在上述代码中,我们为每个Tab设置了自定义的TabView布局,并通过findViewById获取到自定义布局中的TextView来设置文本。 请确保将"R.layout.custom_tab_layout"替换为你自己的自定义TabView布局文件。 同时,你也可以根据需要在自定义TabView布局文件中设置其他样式,如文本颜色、大小等。 这样,你就可以在Android TabLayout设置自定义字体了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值