简体   繁体   English

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

[英]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.

相关问题 在android设计库TabLayout中选择时更改图标和标题颜色 - Change icon and title color when selected in android design library TabLayout 选项卡图标和文本均使用android设计支持库 - Tab icon and text both using android design support library 如何在使用android.support.design.widget.TabLayout的TabLayout时设置特定的Tab并获取Current Tab; - How to set particular Tab and get Current Tab while using TabLayout of android.support.design.widget.TabLayout; TabLayout.Tab的getCustomView()在设计库中不公开 - getCustomView() of TabLayout.Tab is not public in Design Library 在TabLayout(Android设计支持库)中更改选项卡的背景颜色不会占用整个选项卡空间 - Changing the background color of a Tab in TabLayout (Android design support library) doesn't occupy the entire tab space 使用badgeView在选项卡布局中的选项卡图标上添加徽章 - add badge on icon of a tab in a tablayout using badgeView TabLayout不显示带有Android支持库23.2.0的图标 - TabLayout not show icon with android support library 23.2.0 TabLayout(Android 设计库)文本颜色 - TabLayout (Android Design Library) Text Color Android设计库TabLayout背景问题 - Android design library TabLayout background issues 无法在 TabLayout 设计支持库中看到 Tab Indicator - Unable to see Tab Indicator in TabLayout design support library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM