繁体   English   中英

在Android设计库中使用TabLayout的图标选项卡

[英]Tab with icon using TabLayout in Android Design Library

我正在尝试在Android设计库中使用新的TabLayout创建仅带图标的应用栏。

像这样: 在此输入图像描述

如何使用新的TabLayout Android设计库来完成它。

有一个简单的解决方案,或者我只能使用setCustomView。 我试图避免使用它。 因为我没有像上面这张图片那样获得图标的色调。

我试着像这样写:

tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_dashboard))

但是当我选择标签时,图标仍然保持相同的颜色

你必须为图标创建一个selector 例如:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_dashboard_pressed"
          android:state_pressed="true" />
    <item android:drawable="@drawable/ic_dashboard_selected"
          android:state_selected="true" />
    <item android:drawable="@drawable/ic_dashboard_normal" />
</selector>

我像这样使用它:在@Budius中显示的drawable中创建了一个xml文件。

在代码中: tabLayout.getTabAt(0).setIcon(R.drawable.settings_tab_drawable);

等等。

我这样解决了:

tint_tab.xml

<com.hannesdorfmann.appkit.image.TintableImageView
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 app:tint="@color/tab_color_selector"/>

在你的java代码中

TintableImageView tab1 = (TintableImageView) LayoutInflater.from(this).inflate(R.layout.tint_tab, null);
tab1.setImageResource(R.drawable.ic_dummy);
mTabLayout.getTabAt(0).setCustomView(tab1)

参考: https//github.com/sockeqwe/appkit/blob/master/image/src/main/java/com/hannesdorfmann/appkit/image/TintableImageView.java

暂无
暂无

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

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