TabLayout的使用及注意事项(背景色+文字的大小)

Android TabLayout 实战指南
本文详细介绍如何在Android应用中使用TabLayout组件,包括配置依赖、XML布局设置、代码实现、自定义样式等步骤,并提供了修改文字大小和背景色的具体方法。

 

1、需要在build中添加引用的包

compile 'com.android.support:design:25.2.0'

2、布局:

<?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="match_parent"

    android:background="@color/background2"

    android:orientation="vertical">



    <android.support.design.widget.TabLayout

        android:id="@+id/tabs"

        style="@style/TabLayoutStyle"

        android:layout_width="match_parent"

        android:layout_height="@dimen/dp_50"

        android:layout_marginTop="@dimen/dp_6" />



    <android.support.v4.view.ViewPager

        android:id="@+id/vp_view"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_marginTop="@dimen/dp_6" />

</LinearLayout>

注意:在xml中tabs的宽度如果是wrap_content,则会根据文本大小显示多宽

           在xml中tabs的宽度如果是match_parent,则会根据个数平分屏幕的宽度

特别注意:

         要使用TabLayout,Activity主题必须为Theme.AppCompat类型的主题,如:Theme.AppCompat.Light.NoActionBar

         改变选中的背景色和文字大小(如下介绍)

3、代码实现:

主要是viewpager和TabLayout的初始化及绑定,TabLayout的事件处理

 
mViewPagerTab = (ViewPager) findViewById(R.id.vp_view);

mTabLayout = (TabLayout) findViewById(R.id.tabs);

 

 

4、修改TabLayout的文字大小:

 
<style name="TabLayoutTextAppearance" parent="TextAppearance.AppCompat.Widget.ActionBar.Title">

<item name="android:textSize">@dimen/sp_20</item>

</style>

必须这样,否则没有效果

5、修改TabLayout的Item背景色

 
 <!--tab-->

    <style name="TabLayoutStyle" parent="android:Widget">

        <item name="tabBackground">@drawable/tab_backgroud</item>

        <item name="tabIndicatorColor">@color/transparent_color</item>

        <item name="tabIndicatorHeight">@dimen/dp_0</item>

        <item name="tabSelectedTextColor">@color/text_color</item>

        <item name="tabTextColor">@color/tab_text_default_color</item>

        <item name="tabTextAppearance">@style/TabLayoutTextAppearance</item>

    </style>

如果4 问题不那样写,而在5中写是没有效果的

6、选择器的样式

(1)tab_background.xml

 
<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">



    <item android:drawable="@drawable/tab_background_selected" 

        android:state_selected="true" />

    <item android:drawable="@drawable/tab_background_default" 

        android:state_focused="false" 

        android:state_pressed="false" 

        android:state_selected="false" />



</selector>

(2)tab_backgroud_selsected.xml

注意:一定要注意:根是shape--------在没有在意跟的时候,默认是selector,这个选择器是不好用的,坑了我好久。。。

 
<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"

    android:shape="rectangle">

    <!--填充色-->

    <solid android:color="@color/tab_selected_color" />

    <!-- 线的宽度,颜色灰色 -->

    <stroke

        android:width="@dimen/dp_0.5"

        android:color="@color/background2"></stroke>

    <!-- 矩形的圆角半径 -->

    <corners android:radius="@dimen/dp_10" />

</shape>

(3)tab_backgroud_default.xml

 
<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <solid android:color="@color/background2" />

</shape>

有的是在(1)中选择的时候是color="直接颜色值",当时drawable的时候一定注意根是shape

 

尊重作者劳动成果:

https://blog.youkuaiyun.com/suyimin2010/article/details/81805141

在 Android 的 `TabLayout` 中,文字大小不一致的问题通常是由系统默认样式、主题或字体设置引起的。尤其是在不同设备或系统版本上,Tab 文字可能会因为系统默认的 `textAppearance` 而出现不统一的情况。为了解决这个问题,可以通过以下几种方式来实现统一的 Tab 文字大小。 ### 1. 使用 XML 中的 `app:tabTextAppearance` 属性 在布局文件中直接设置 `TabLayout` 的 `tabTextAppearance` 属性,可以统一所有 Tab 的文本样式,包括字体大小、颜色等。 ```xml <com.google.android.material.tabs.TabLayout android:id="@+id/tabLayout" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabTextAppearance="@style/CustomTabTextAppearance" /> ``` 在 `styles.xml` 中定义自定义样式: ```xml <style name="CustomTabTextAppearance" parent="TextAppearance.MaterialComponents.Body2"> <item name="android:textSize">14sp</item> <item name="android:textColor">@color/black</item> </style> ``` ### 2. 在代码中动态设置 Tab 文字大小 如果希望在运行时动态控制 Tab 的文字大小,可以通过获取每个 Tab 的 `TextView` 并手动设置字体大小。 ```kotlin val tabLayout: TabLayout = findViewById(R.id.tabLayout) tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener { override fun onTabSelected(tab: TabLayout.Tab?) { tab?.view?.findViewById<TextView>(com.google.android.material.R.id.text1)?.apply { textSize = 14f // 设置统一字体大小 } } override fun onTabUnselected(tab: TabLayout.Tab?) {} override fun onTabReselected(tab: TabLayout.Tab?) {} }) ``` ### 3. 自定义 TabView 布局 通过自定义每个 Tab 的布局,可以完全控制其外观,包括文字大小、字体、颜色等。 ```xml <!-- res/layout/tab_item.xml --> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tabTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="14sp" android:textColor="@color/black" android:gravity="center"/> ``` 在代码中加载自定义布局: ```kotlin val tabLayout: TabLayout = findViewById(R.id.tabLayout) val titles = listOf("Tab 1", "Tab 2", "Tab 3") titles.forEach { title -> val tab = tabLayout.newTab() val view = layoutInflater.inflate(R.layout.tab_item, null) val textView = view.findViewById<TextView>(R.id.tabTextView) textView.text = title tab.customView = view tabLayout.addTab(tab) } ``` ### 4. 避免系统默认样式干扰 如果发现某些设备上文字大小仍然不一致,可以检查是否应用了系统默认的 `textAppearance` 样式。可以在 `TabLayout` 中显式设置 `app:tabTextAppearance` 来覆盖系统默认值。 ### 5. 使用 `ViewCompat.setPaddingRelative()` 避免文字被截断 在某些设备上,Tab 文字可能会因为宽度不足而被截断,可以通过设置内边距来改善显示效果: ```kotlin val tabView = tabLayout.getTabAt(0)?.view ViewCompat.setPaddingRelative(tabView, 16, 0, 16, 0) ``` ### 6. 注意事项 - **Tab 宽度问题**:如果 Tab 文字较多,可能会导致 Tab 拥挤,可以考虑使用 `app:tabMode="scrollable"` 让 Tab 支持水平滚动。 - **点击区域问题**:自定义 Tab 时,确保点击区域完整,避免因 `margin` 或 `padding` 导致点击区域不完整。 通过上述方法,可以有效解决 Android `TabLayout` 中文字大小不一致的问题,确保在不同设备和系统版本上都能保持一致的显示效果。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值