TabLayout使用以及自定义tab标签

kotlin 复制代码
<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:tabTextAppearance="@style/myTab"

        style="@style/myTabLayout"
        tools:ignore="SpeakableTextPresentCheck" />

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/vp"

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toBottomOf="@+id/tab" />


</androidx.constraintlayout.widget.ConstraintLayout>
kotlin 复制代码
package com.example.myapplication


import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Lifecycle
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.example.myapplication.databinding.TestVp2Binding
import com.google.android.material.tabs.TabLayoutMediator


class VP2Demo : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)


        val inflate = TestVp2Binding.inflate(layoutInflater)
        setContentView(inflate.root)


        val mutableListOf = mutableListOf<Fragment>()
        val fragment = Fragment(R.layout.test_a)

        val fragment2 = Fragment(R.layout.test_view)


        val fragment3 = Fragment(R.layout.test_net)


        val childAt = inflate.vp.getChildAt(0)
        (childAt as? RecyclerView)?.setOverScrollMode(View.OVER_SCROLL_NEVER)
        mutableListOf.add(fragment)
        mutableListOf.add(fragment2)
        mutableListOf.add(fragment3)

        inflate.vp.adapter = MyVpAdapter(mutableListOf, supportFragmentManager, lifecycle)
        val titles = mutableListOf<String>()
        titles.add("新闻")
        titles.add("新闻2")
        titles.add("新闻3")



        TabLayoutMediator(
            inflate.tab, inflate.vp
        ) { p0, index ->
            run {
                p0.setText(titles[index])
                p0.setIcon(R.drawable.zc)
                if(index==1){
                    val textView = TextView(baseContext)
                    textView.text = "我是自定义的标签哈哈~~~"
                    p0.setCustomView(textView)
                }

            }
        }.attach()




    }


    class MyVpAdapter(list: MutableList<Fragment>, fm: FragmentManager, lf: Lifecycle) :
        FragmentStateAdapter(fm, lf) {

        private var listS = emptyList<Fragment>()

        init {
            listS = list

        }

        override fun getItemCount(): Int {
            return listS.size
        }

        override fun createFragment(position: Int): Fragment {

            return listS[position]
        }

    }

}
kotlin 复制代码
  <style name="myTab" parent="@android:style/TextAppearance.Widget.TabWidget">
        <item name="textAllCaps">false</item>
        <item name="android:textSize">30sp</item>



    </style>
    <style name="myTabLayout" parent="Widget.Design.TabLayout">
        <item name="tabIndicatorColor">#00FF00</item>
        <item name="tabSelectedTextColor">#FF0000</item>

        <item name="tabTextColor">#0000FF</item>
        <item name="tabRippleColor">@color/yellow</item>

    </style>
相关推荐
Dnelic-2 小时前
【单元测试】【Android】JUnit 4 和 JUnit 5 的差异记录
android·junit·单元测试·android studio·自学笔记
Eastsea.Chen4 小时前
MTK Android12 user版本MtkLogger
android·framework
长亭外的少年12 小时前
Kotlin 编译失败问题及解决方案:从守护进程到 Gradle 配置
android·开发语言·kotlin
JIAY_WX12 小时前
kotlin
开发语言·kotlin
建群新人小猿14 小时前
会员等级经验问题
android·开发语言·前端·javascript·php
1024小神15 小时前
tauri2.0版本开发苹果ios和安卓android应用,环境搭建和最后编译为apk
android·ios·tauri
兰琛15 小时前
20241121 android中树结构列表(使用recyclerView实现)
android·gitee
Y多了个想法16 小时前
RK3568 android11 适配敦泰触摸屏 FocalTech-ft5526
android·rk3568·触摸屏·tp·敦泰·focaltech·ft5526
NotesChapter17 小时前
Android吸顶效果,并有着ViewPager左右切换
android
_祝你今天愉快18 小时前
分析android :The binary version of its metadata is 1.8.0, expected version is 1.5.
android