安卓笔记:自定义TabLayout

TabLayout用于实现标签布局。类似下图:

原生的TabLayout支持文字和图标,不过图标大小不可调,颜色也比较单调,为了美观,需要自定义。

下面是最新版的 TabLayout

   <com.google.android.material.tabs.TabLayout
        android:id="@+id/bottom_tab_layout"
        android:layout_width="match_parent"
        app:tabIndicatorHeight="0dp"
        android:background="@android:color/black"
        android:layout_height="50dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        >

    </com.google.android.material.tabs.TabLayout>

使用前,需要导入 下包

 implementation 'com.google.android.material:material:1.5.0'

自定义每个标签的样式,创建 layout 资源文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.pingbu.nban.activity.ui.IconFontTextView
        android:id="@+id/bottom_menu_item_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="2dp"
        android:text="@string/iconfont_hangqing"
        android:textColor="@color/white"

        android:textSize="22dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        >

    </com.pingbu.nban.activity.ui.IconFontTextView>

    <TextView
        android:id="@+id/bottom_menu_item_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="2dp"
        android:text="@string/hangqing"
        android:textColor="@color/white"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/bottom_menu_item_img"
        >

    </TextView>

</androidx.constraintlayout.widget.ConstraintLayout>

这里当然应该创建多个,我省略了,就做了一个演示一下。上面是图标文字 IconFont ,下面是普通文字。当然,作为演示,上面本不应该用 IconFont标签的,那个是我自定义的标签,我会在后续中详细介绍 IconFont 的使用。

在 主 Activity 的代码中如下:

package com.pingbu.nban.activity

import android.graphics.Typeface
import android.os.Bundle
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.tabs.TabLayout
import com.pingbu.nban.R
import kotterknife.bindView


class MainActivity : AppCompatActivity() {


    private val mTabLayout: TabLayout? by bindView<TabLayout>(R.id.bottom_tab_layout)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        initView()
    }

    private fun initView(){

        val tab1 = mTabLayout!!.newTab().setCustomView(R.layout.bottom_item1)
        mTabLayout!!.addTab(tab1);

        val tab2 = mTabLayout!!.newTab().setCustomView(R.layout.bottom_item2)
        mTabLayout!!.addTab(tab2);

        mTabLayout!!.addTab(mTabLayout!!.newTab().setCustomView(R.layout.bottom_middle_item));

        val tab3 = mTabLayout!!.newTab().setCustomView(R.layout.bottom_item3)
        mTabLayout!!.addTab(tab3);

        val tab4 = mTabLayout!!.newTab().setCustomView(R.layout.bottom_item4)
        mTabLayout!!.addTab(tab4);

        val tabSelect:TabLayout.OnTabSelectedListener = object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                val view1 = tab!!.view
                if(view1!! == null){
                    return;
                }
                val txt1 = view1.findViewById<TextView>(R.id.bottom_menu_item_txt)
                if(txt1!! == null){
                    return;
                }

                txt1.setTextColor(getResources().getColor(R.color.orang1))

                val txtImg1 = view1.findViewById<TextView>(R.id.bottom_menu_item_img)
                txtImg1.setTextColor(getResources().getColor(R.color.orang1))

            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
                val view1 = tab!!.view
                if(view1!! == null){
                    return;
                }
                val txt1 = view1.findViewById<TextView>(R.id.bottom_menu_item_txt)
                if(txt1!! == null){
                    return;
                }

                txt1.setTextColor(getResources().getColor(R.color.white))

                val txtImg1 = view1.findViewById<TextView>(R.id.bottom_menu_item_img)
                txtImg1.setTextColor(getResources().getColor(R.color.white))

            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
                val view1 = tab!!.view
                if(view1!! == null){
                    return;
                }
                val txt1 = view1.findViewById<TextView>(R.id.bottom_menu_item_txt)
                if(txt1!! == null){
                    return;
                }

                txt1.setTextColor(getResources().getColor(R.color.orang1))

                val txtImg1 = view1.findViewById<TextView>(R.id.bottom_menu_item_img)
                txtImg1.setTextColor(getResources().getColor(R.color.orang1))

            }

        }

        mTabLayout!!.addOnTabSelectedListener(tabSelect)

    }


    override fun onBackPressed() {
        super.onBackPressed()
        android.os.Process.killProcess(android.os.Process.myPid());
    }
}

上面的代码显示了,如何向 TabLayout 中添加多个 自定义的Tab标签, 这里用到了  bottom_item1 bottom_item2   bottom_item3  bottom_item4  以及 bottom_middle_item ,除了 bottom_item1 的脚本代码其他的我都省略了。

同时,由于自定义控制了,标签的颜色等都没法切换,所以,我自定义了 TabLayout的OnTabSelectedListener ,自定义了选中、不选中、再次选中的字体颜色。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小崔爱读书

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值