[英]Tab with icon using TabLayout in Android Design Library
I'm trying to use the new TabLayout in the android design library to create app bar with icons only. 我正在尝试在Android设计库中使用新的TabLayout创建仅带图标的应用栏。
like this: 像这样:
how can I do it using the new TabLayout Android Design Library. 如何使用新的TabLayout Android设计库来完成它。
is there a simple solution for this, or i have to use the setCustomView only. 有一个简单的解决方案,或者我只能使用setCustomView。 i'm trying to avoid using it. 我试图避免使用它。 because i didn't get the tint color for the icon like this image above. 因为我没有像上面这张图片那样获得图标的色调。
i try to write like this: 我试着像这样写:
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_dashboard))
but the icon still stay in the same color when i select the tab 但是当我选择标签时,图标仍然保持相同的颜色
you have to create a selector
for the icon. 你必须为图标创建一个selector
。 For example: 例如:
<?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>
I used it like this: created an xml file in drawable as shown by @Budius. 我像这样使用它:在@Budius中显示的drawable中创建了一个xml文件。
in the code: tabLayout.getTabAt(0).setIcon(R.drawable.settings_tab_drawable);
在代码中: tabLayout.getTabAt(0).setIcon(R.drawable.settings_tab_drawable);
and so on. 等等。
i solved it like this: 我这样解决了:
tint_tab.xml 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"/>
in you java code 在你的java代码中
TintableImageView tab1 = (TintableImageView) LayoutInflater.from(this).inflate(R.layout.tint_tab, null);
tab1.setImageResource(R.drawable.ic_dummy);
mTabLayout.getTabAt(0).setCustomView(tab1)
ref: https://github.com/sockeqwe/appkit/blob/master/image/src/main/java/com/hannesdorfmann/appkit/image/TintableImageView.java 参考: 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.