【问题标题】:How to customize a Material Tab to show its title when its selected only?如何自定义材质选项卡以仅在选择时显示其标题?
【发布时间】:2023-03-13 09:35:01
【问题描述】:

我想制作一个自定义材料选项卡,该选项卡只显示选项卡本身时显示其文本。否则图标应该单独显示。

此外,图标应该重新分配可用空间,并且只占用所需的空间。

示例视频:https://storage.googleapis.com/spec-host-backup/mio-design%2Fassets%2F1SkeVD6imJA_MwjipNR-DbNh1xw4au9yy%2F02-rally-tabs.mp4

我怎样才能意识到这一点?

【问题讨论】:

  • 您的问题不清楚。如果要显示标题,为什么要隐藏TextView?
  • @Froyo 对不起,“唯一”的位置错误!未选中选项卡时,应显示 iicon,选中时应显示图标+文本。
  • 如果您想在未选择选项卡时隐藏文本,hacky 方法是设置自定义tabTextAppearance,在其中创建样​​式并添加textColor .在这个textColor 选择器中,当文本未被选中时使用标签颜色。
  • 你试过什么?您是否尝试在未选中时将选项卡标题设置为 ""
  • 您可以使用反射来访问选项卡上的 texview,但不建议这样做,因为它是私有的,并且可能会随着 Android 更新而改变......如果我没记错的话,TexView 被称为title。因此,如果您获得选项卡并执行getRoot().findViewById(android.R.id.title),您应该能够操作它。同样,推荐。检查此示例:stackoverflow.com/a/2292323/761668

标签: android tabs android-tabs material-components-android


【解决方案1】:

您可以使用TabLayout.getTab(position) 来获取已选择的选项卡和未选择的选项卡。一旦你有了Tab 实例,你就可以调用Tab.setText("") 这应该会删除标签的标题。

val titles = listOf("tab1", "tab2", "tab3")

val tabs = titles.map {
    tabLayout.newTab().also {
        tabLayout.addTab(it)
        it.setIcon(R.mipmap.ic_launcher)
    }
}

tabLayout.addOnTabSelectedListener(object: TabLayout.OnTabSelectedListener {
    override fun onTabReselected(tab: TabLayout.Tab?) {
    }

    override fun onTabUnselected(tab: TabLayout.Tab?) {
        tab?.text = ""
    }

    override fun onTabSelected(tab: TabLayout.Tab?) {
        val index = tabs.indexOfFirst { it == tab }
        tab?.text = titles[index]

    }

})

更新

您可以通过为您的 Tab 使用自定义视图来实现该效果。

1 为您的选项卡创建视图布局。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    tools:layout_height="48dp"
    android:padding="4dp">

    <ImageView
        android:id="@+id/tab_icon"
        android:layout_width="36dp"
        android:layout_height="36dp"
        tools:srcCompat="@mipmap/ic_launcher"/>

    <TextView
        android:id="@+id/tab_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:padding="8dp"
        android:textSize="18sp"
        tools:text="Tab title"/>

</LinearLayout>

2 在您的 Tab 中使用自定义视图

val tabs = titles.map {
    tabLayout.newTab().also {
        tabLayout.addTab(it)
        val tabView = LayoutInflater.from(this).inflate(R.layout.tab_item_layout, tabLayout, false)
        tabView.findViewById<ImageView>(R.id.tab_icon).setImageResource(R.mipmap.ic_launcher)
        tabView.findViewById<TextView>(R.id.tab_title).text = title
        it.customView = tabView
    }
}

3 选择选项卡时更新标题的可见性。

tabLayout.addOnTabSelectedListener(object: TabLayout.OnTabSelectedListener {
    override fun onTabReselected(tab: TabLayout.Tab?) {
    }

    override fun onTabUnselected(tab: TabLayout.Tab?) {
        tab?.customView?.findViewById<TextView>(R.id.tab_title)?.apply {
            visibility = View.GONE
        }
    }

    override fun onTabSelected(tab: TabLayout.Tab?) {
        tab?.customView?.findViewById<TextView>(R.id.tab_title)?.apply {
            visibility = View.VISIBLE
        }

    }

})

4 在您用 XML 声明 TabLayout 时,您需要添加 app:tabMode="scrollable"

【讨论】:

  • Tabs 是否可以重新分配空间,从而使所选选项卡的完整标签可见?
  • 应该是可以的。您需要使用tab.setCustomView() 为您的标签使用自定义视图,然后您将拥有更大的灵活性。
猜你喜欢
  • 2018-02-07
  • 2017-06-27
  • 1970-01-01
  • 2022-01-18
  • 2019-07-16
  • 2016-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多