【问题标题】:Android Studio: Tab indicator activeAndroid Studio:选项卡指示器处于活动状态
【发布时间】:2020-01-12 16:24:45
【问题描述】:

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

这是我的 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>

这就是我想要做的,基本上。

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

【问题讨论】:

    标签: java android android-studio kotlin


    【解决方案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) {
    
             }
         }
    
    })
    

    【讨论】:

      猜你喜欢
      • 2020-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-30
      • 1970-01-01
      • 2020-04-24
      • 2019-01-15
      • 2011-10-22
      相关资源
      最近更新 更多