Java Android Studio:选项卡指示器处于活动状态

Java Android Studio:选项卡指示器处于活动状态,java,android,android-studio,kotlin,Java,Android,Android Studio,Kotlin,因此,我想通过编程使选项卡指示器颜色处于活动状态。我在顶部有一个进度条,我希望以前选中的每个选项卡都保持彩色(或选中) 这是我的XML,尽管我怀疑它是否有用 <?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:a

因此,我想通过编程使选项卡指示器颜色处于活动状态。我在顶部有一个进度条,我希望以前选中的每个选项卡都保持彩色(或选中)

这是我的XML,尽管我怀疑它是否有用

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
    tools:context=".FinishProfile">



    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="5dp"
            android:background="@android:color/white"
            app:tabIndicatorColor="@android:color/holo_blue_light"
            app:tabIndicatorHeight="5dp"
            app:tabRippleColor="@android:color/white"
            app:tabTextColor="@android:color/white">




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

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />


</androidx.coordinatorlayout.widget.CoordinatorLayout>
我想它会起作用的

tabs!!.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener{

     override fun onTabSelected(tab: TabLayout.Tab?) {
         viewPager!!.currentItem = tab?.position!!    
         if (tab.position == 0) {
             //like this: tabs.setSelectedTabIndicatorColor(Color.BLUE)
             //or like this:  tabs.getTabAt(0)!!.getIcon()!!.setAlpha(100)

         }else if (tab.position == 1) {

         }
     }

})
tabs!!.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener{

     override fun onTabSelected(tab: TabLayout.Tab?) {
         viewPager!!.currentItem = tab?.position!!    
         if (tab.position == 0) {
             //like this: tabs.setSelectedTabIndicatorColor(Color.BLUE)
             //or like this:  tabs.getTabAt(0)!!.getIcon()!!.setAlpha(100)

         }else if (tab.position == 1) {

         }
     }

})