简体   繁体   English

Android Studio:标签指示器处于活动状态

[英]Android Studio: Tab indicator active

So I want to programatically make the tab indicator color active.所以我想以编程方式使选项卡指示器颜色处于活动状态。 I have a sort of progress bar at the top, and I want each tab that has been selected previously to stay colored (or selected).我在顶部有一个进度条,我希望以前选择的每个选项卡保持彩色(或选中)。

Here is my XML, although I doubt it helps.这是我的 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>

And here is what I want to do, essentially.这就是我想要做的,本质上。

val tab1 = tabs.getTabAt(0)
// I need something like: tab1.tabIndicatorActive = true

I think it will work我认为它会起作用

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) {

         }
     }

})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM