google tablayout设置字体大小

<style name="MyCustomTabText" parent="TextAppearance.AppCompat.Button">
    <item name="android:textSize">15dp</item>
    <item name="android:textColor">@color/c_9796A9</item>
</style>
<com.google.android.material.tabs.TabLayout
app:tabTextAppearance="@style/MyCustomTabText"
/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在 Android TabLayout 中设置字体,可以使用自定义的 TabLayout.TabView。 首先,创建一个自定义的 TabView 类,继承自 TabLayout.TabView: ```java import android.content.Context; import android.graphics.Typeface; import android.util.AttributeSet; import com.google.android.material.tabs.TabLayout; public class CustomTabView extends TabLayout.TabView { public CustomTabView(Context context) { super(context); } public CustomTabView(Context context, AttributeSet attrs) { super(context, attrs); } public CustomTabView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public void setSelected(boolean selected) { super.setSelected(selected); updateTabFont(); } private void updateTabFont() { Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), "your_font.ttf"); setTypeface(typeface); } } ``` 注意,上述代码中的 "your_font.ttf" 是你自己的字体文件名,你需要将字体文件放置在 `assets` 文件夹中。 接下来,在你的 Activity 或 Fragment 中,使用自定义的 TabView 替代原始的 TabView: ```java TabLayout tabLayout = findViewById(R.id.tab_layout); tabLayout.clearOnTabSelectedListeners(); for (int i = 0; i < tabLayout.getTabCount(); i++) { TabLayout.Tab tab = tabLayout.getTabAt(i); if (tab != null) { tab.setCustomView(R.layout.custom_tab_view); View customTabView = tab.getCustomView(); if (customTabView instanceof CustomTabView) { ((CustomTabView) customTabView).updateTabFont(); } } } ``` 以上代码会将每个 Tab 的布局替换为自定义的 TabView,并在每次选择 Tab 时更新字体样式。 最后,创建一个名为 `custom_tab_view.xml` 的布局文件,用于自定义 Tab 的样式: ```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:textAppearance="?attr/textAppearanceTab" android:textColor="@color/tab_text_color" android:textSize="14sp" /> ``` 请确保在 `custom_tab_view.xml` 中设置适当的文本样式和颜色。 这样,你就可以使用自定义的字体样式来设置 Android TabLayout 中的字体了。记得将 "your_font.ttf" 替换为你自己的字体文件名。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值