代码之家  ›  专栏  ›  技术社区  ›  Martin

TabLayout所选文本颜色同时显示在两个选项卡中

  •  -1
  • Martin  · 技术社区  · 6 年前

    我已经使用ViewPager进行了tabLayout,但是当我选择一个选项卡时,以前选择的选项卡文本颜色也将保持为选定的颜色。所以我最终得到了两个处于选定文本状态的选项卡。如您所见,我让onTabSelectedListener将图标从选中状态更改为未选中状态,它工作正常。只有文字是个问题。我以前也尝试过为tabLayout制作xml选择器,但最终没有成功。

    更新 -当我删除tabLayout.addOnTabSelectedListener时,文本颜色切换正确。但这只是部分地解决了我的问题。现在,我必须找到解决方案图标交换时,选项卡被选中。

    tab.icon = getDrawable(iconListActive[viewPager.currentItem])
    

    这个精确的代码屏蔽了颜色切换。但这没有意义。如我所说 super.onTabSelected(tab) 颜色切换非常有效。

    TabLayout和ViewPager初始化:

    private var pagerAdapter: ViewPagerAdapter? = null
    private lateinit var viewPager: CustomViewPager
    private lateinit var tabLayout: TabLayout
    
    tabLayout = findViewById(R.id.tab_layout)
    viewPager = findViewById(R.id.viewPager)
    
    tabLayout.setTabTextColors(
                    resources.getColor(R.color.color_grey),
                    resources.getColor(R.color.color_green)
            )
    
    pagerAdapter = ViewPagerAdapter(supportFragmentManager, this)
    pagerAdapter!!.apply {
            addFragment(FirstFragment(), "Tab 1")
            addFragment(SecondFragment(), "Tab 2")
            addFragment(ThirdFragment(), "Tab 3")
            addFragment(FourthFragment(), "Tab 4")
        }
    
    viewPager.adapter = pagerAdapter
    
    tabLayout.setupWithViewPager(viewPager)
    tabLayout.tabGravity = (TabLayout.GRAVITY_FILL)
    
    tabLayout.addOnTabSelectedListener(object : TabLayout.ViewPagerOnTabSelectedListener(viewPager) {
                override fun onTabSelected(tab: TabLayout.Tab) {
                    tab.icon = getDrawable(iconListActive[viewPager.currentItem])
    
                }
    
                override fun onTabUnselected(tab: TabLayout.Tab) {
                    tab.icon = getDrawable(iconList[viewPager.currentItem])
    
                }
    
            })
    
        }
    

    自定义网页

    import android.content.Context
    import android.support.v4.view.ViewPager
    import android.util.AttributeSet
    import android.view.MotionEvent
    
    
    class CustomViewPager(context: Context, attributeSet: AttributeSet) : ViewPager(context, attributeSet) {
    
        override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
            return false
        }
    
        override fun onTouchEvent(ev: MotionEvent?): Boolean {
            return false
        }
    }
    

    视图页面

    class ViewPagerAdapter(fm: FragmentManager, private val ctx: Context) : FragmentStatePagerAdapter(fm) {
    
        private val fragments = ArrayList<Fragment>()
        private val tabTitles = ArrayList<String>()
    
    
        override fun getItem(position: Int): Fragment {
            return fragments[position]
        }
    
        override fun getCount(): Int {
            return fragments.size
        }
    
        fun addFragment(fragment: Fragment, tabtitle: String){
            fragments.add(fragment)
            tabTitles.add(tabtitle)
        }
    
        override fun getPageTitle(position: Int): CharSequence {
            return tabTitles[position]
        }
    

    TabLayout和ViewPager XML

    <FrameLayout
            android:id="@+id/frameContainer"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toTopOf="@+id/tab_layout"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
    
            <com.app.CustomViewPager
                android:id="@+id/viewPager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/colorBackground" />
        </FrameLayout>
    
    <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            style="@style/TabBarTheme"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:elevation="4dp"
            android:visibility="visible"
            android:minHeight="?attr/actionBarSize"
            android:layout_alignParentBottom="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />
    

    选项卡样式XML:

    <style name="TabBarTheme" parent="Widget.Design.TabLayout">
            <item name="android:background">@color/colorBackground</item>
            <item name="tabTextAppearance">@style/AppTabTextAppearance</item>
            <item name="tabIndicatorHeight">0dp</item>
        </style>
    
    <style name="AppTabTextAppearance" parent="TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse">
            <item name="textAllCaps">false</item>
            <item name="android:capitalize">words</item>
            <item name="android:textSize">10sp</item>
        </style>
    

    该行为的图形表示(不能发布实际图形)

    enter image description here

    2 回复  |  直到 6 年前
        1
  •  0
  •   Manish Singh Rana    6 年前

    使用以下样式代码更改选择时TabLayout中文本的颜色。

    <style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
        <item name="tabIndicatorColor">@color/colorPrimary</item>
        <item name="tabTextAppearance">@style/MyCustomTabText</item>
        <item name="tabSelectedTextColor">@color/colorPrimary</item>
    </style>
    
    <style name="MyCustomTabText" parent="TextAppearance.AppCompat.Button">
        <item name="android:textSize">14sp</item>
        <item name="android:textColor">@android:color/black</item>
        <item name="android:textAllCaps">false</item>
    </style>
    

    在你的小报上使用这种风格。

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        style="@style/MyCustomTabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="4dp"
        android:visibility="visible"
        android:minHeight="?attr/actionBarSize"
        android:layout_alignParentBottom="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
    
        2
  •  0
  •   Chú Tiên    6 年前

    您需要使用CustomView 表布局 并将选项卡CustomView的选择器设置为false 被选中的 . 如下所示:

    View view = tab.getCustomView();
    if (view != null) {
         tab.icon = getDrawable(iconList[viewPager.currentItem])
         view.setSelected(false);
    }