Android:如何在一个向量Drawable xml中使用选择器更改android:fillcolor [英] Android: How to change android:fillcolor with selector in one Vector Drawable xml

查看:450
本文介绍了Android:如何在一个向量Drawable xml中使用选择器更改android:fillcolor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标签图标:我当前的方法是创建两个文件(ic_list_selected_24dp.xml和ic_list_unselected_24dp.xml;它们基本相同,但只有 android:fillColor ='Color HEX CODE'是不同的),然后创建一个选择器(selector_tabitem_list.xml)以在状态更改时更改可绘制颜色。

Tab Icons: My current method is to create two files (ic_list_selected_24dp.xml and ic_list_unselected_24dp.xml; they are basically the same but only the android:fillColor='Color HEX CODE' are different), and then create a selector (selector_tabitem_list.xml) to change the drawable color when the state is changed.

// @drawable/selector_tabitem_list.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:drawable="@drawable/ic_list_selected_24dp" 
        android:state_selected="true" />
    <item android:drawable="@drawable/ic_list_unselected_24dp" 
        android:state_selected="false" />
</selector>

有点重复,因为两个可绘制对象相同。

It's kind of duplicated because two drawables are the same.

选择器不能用于矢量可绘制对象。

Selector cannot be used in vector drawable.

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24.0"
    android:viewportHeight="24.0">
    <path
        android:fillColor="@drawable/selector"
        android:pathData="M19,3...."
</vector>

-

// @drawable/selector

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true">
        <color android:color="@color/itemSelected" />
    </item>
    <item android:state_selected="false">
        <color android:color="@color/itemUnselected" />
    </item>
</selector>

android:fillColor = @ color / state

, and android:fillColor="@color/state" either.

// @color/state

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@android:color/white" android:state_selected="true" />
    <item android:color="@android:color/black" android:state_selected="false" />
</selector>

有没有办法使用一个可绘制对象并动态更改其颜色?
使用硬十六进制代码更好吗?

Is there any way to use one drawable and change its color dynamically? Using hard hex code is better?

谢谢。

推荐答案

以下是在TabItem(属于支持设计库的一部分)中使用矢量资产作为有色图标的步骤的完整列表。所有部分都存在于原始问题和链接的答案中,但很容易错过。

Here is the complete list of steps to use a vector asset as tinted icon in a TabItem (which is part of the support design lib). All parts are present in the original question and linked answer, but are easy to miss.


  1. 创建选择器。请注意,它必须切换 state_selected 的颜色(包括在问题中,但不包含在@cmingmai链接的答案中。那里只声明了android:state_enabled而不是与标签相关):

  1. Create the selector. Note that it has to switch the color for state_selected (as included in the question, but not in the answer linked by @cmingmai. There it only states android:state_enabled which is not relevant for tabs):

res / color / selector_navigation.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="false" android:color="@android:color/black" />
    <item android:state_selected="true" android:color="@android:color/white" />
</selector>




  1. 通过添加<$ c来调整可绘制矢量将$ c> android:tintMode android:tint 添加到vector标签。
    为了使着色与乘法配合使用,路径的fillColor需要设置为 white

  1. Adjust the vector drawable by adding android:tintMode and android:tint to the vector tag. In addition for the tinting to work with multiply, the fillColor of the path needs to be set to white!

res / drawable / ic_tab_list:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:height="24dp"
        android:width="24dp"
        android:viewportHeight="24.0"
        android:viewportWidth="24.0"
        android:tintMode="multiply"
        android:tint="@color/selector_navigation">
    <path
        android:fillColor="@android:color/white"
        android:pathData="..." />
</vector>




  1. 在布局中使用矢量可绘制–或用于按照标签开发人员指南所示的代码创建标签。请注意,我还修改了选项卡指示器颜色以使其与活动图标颜色匹配,以遵循材料设计准则

  1. Use vector drawable in Layout – or use to create the tabs in code as shown in the developer guide on Tabs. Note that I also modified the tab indicator color to match the active icon color to follow the material design guidelines.

布局的相关部分:

<android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabIndicatorColor="@android:color/white">

    <android.support.design.widget.TabItem
        android:id="@+id/tabItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:icon="@drawable/ic_tab_list" />
    <!-- More Tabs -->
</android.support.design.widget.TabLayout>




  1. 调整内部版本。 gradle ,并通过以下步骤激活对旧Android版本的向量支持(如果以前尚未添加):

  1. Adjust build.gradle with following to activate vector support for old Android versions (in case it was not already previously added):

android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}

android { defaultConfig { vectorDrawables.useSupportLibrary = true } }

这篇关于Android:如何在一个向量Drawable xml中使用选择器更改android:fillcolor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆