api <21的可绘制着色

Oro*_*chi 79 android

是否有可能为api <21进行可绘制的着色工作?

<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/ic_calendar"
    android:tint="@color/primary" />
Run Code Online (Sandbox Code Playgroud)

工作正常但仅适用于具有API21的设备.针对较低api设备或AppCompat支持的任何解决方法?找不到任何东西.

Sak*_*boy 103

使用AppCompatImageView如下:

<android.support.v7.widget.AppCompatImageView
        android:id="@+id/my_appcompat_imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/my_image"
        android:tint="#636363"
    />
Run Code Online (Sandbox Code Playgroud)

确保您拥有最新appcompat-v7的应用程序build.gradle.

示例:compile 'com.android.support:appcompat-v7:25.0.0'在您的应用中build.gradle.

  • 来自`AppCompatImageView` docs:`当你在布局中使用ImageView时,这将自动使用.编写自定义视图时,您只需手动使用此类.http://developer.android.com/reference/android/support/v7/widget/AppCompatImageView.html因此,在布局中使用正常的`ImageView`应该工作得很好. (61认同)
  • 这在使用appcompat-v7:25.1.0的模拟器Android 4.0上不起作用. (4认同)
  • AppCompatImageView不能在Widget中使用.在ImageView上使用setColorFilter. (4认同)

Sim*_*ges 42

您可以使用源代码实现这一目标.之前的着色不受支持DrawableCompat.从支持库22.1开始,您可以这样做,但您需要以这种方式执行此操作:

Drawable normalDrawable = getResources().getDrawable(R.drawable.drawable_to_tint);
Drawable wrapDrawable = DrawableCompat.wrap(normalDrawable);
DrawableCompat.setTint(wrapDrawable, getResources().getColor(R.color.colorPrimaryLight));
Run Code Online (Sandbox Code Playgroud)

  • 如果你需要支持<21个API的着色,那么你可能需要使用`ContextCompat.getColor()`,而不是`getResources().的getColor()`. (16认同)

Jon*_*nik 22

难道你不能简单地使用ImageView来显示你的Drawable吗?android:tint适用于较旧的API级别.

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_calendar"
    android:tint="@color/primary"
    />
Run Code Online (Sandbox Code Playgroud)

  • 同样的问题在这里 可悲的是,选择器的色调不适用于api <21 (3认同)
  • 我正在使用ImageView - 在其中显示图标.这些图标是导航抽屉中元素的一部分.导航抽屉中的所选项目具有不同的颜色,因此我创建了每个图标,并为每个图标创建了选择器.我正在使用该选择器作为我的图标.选择器:`<选择器xmlns:android ="http://schemas.android.com/apk/res/android"> <item android:state_activated ="true"android:drawable ="@ drawable/ic_home_tinted"/> <item android:drawable ="@ drawable/ic_home"/> </ selector>` (2认同)

Jar*_*ows 17

此前有人问过类似的问题:https://stackoverflow.com/a/26533340/950427

Android Drawable Tinting仅在Android 5.0+(API 21+)中受支持.(确实说" At the moment this is limited to coloring the action bar and some widgets.").

主题化

...

设置这些属性时,AppCompat会自动将其值传播到API 21+上的框架属性.这会自动为状态栏和概述(最近)任务条目着色.

在较旧的平台上,AppCompat尽可能模拟颜色主题.目前,这仅限于为操作栏和一些小部件着色.

小工具着色

在使用Android 5.0的设备上运行时,所有小部件都使用我们刚才谈到的颜色主题属性进行着色.在Lollipop上有两个主要功能:drawable着色,并在drawables中引用主题属性(形式为?attr/foo).

AppCompat在早期版本的Android上为UI小部件的子集提供了类似的行为:

AppCompat的工具栏(动作模式等)提供的一切EditText Spinner CheckBox RadioButton Switch(使用新的android.support.v7.widget.SwitchCompat)CheckedTextView你不需要做任何特别的事情来使这些工作,只需使用这些控件像往常一样你的布局和AppCompat将完成其余的工作(有一些注意事项;请参阅下面的常见问题解答).

资料来源:

http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.html

https://chris.banes.me/2014/10/17/appcompat-v21/


Ank*_*ngh 13

现在AppCompatImageView,AppCompatButton将取代ImageView,Button以支持具有较低API的设备上的色调.检查链接以获取更多详细信息 AppCompatImageView,AppCompatButton


Pet*_*rdk 6

对于您可以使用的着色图像imageView.setColorFilter(int color).这适用于API 8,用于将我的黑色图像着色为我想要的颜色.这可以取代,setImageTintList()但只是使用android:tint也应该工作.


sei*_*nta 6

使用这个命名空间
xmlns:app="http://schemas.android.com/apk/res-auto"

然后你可以用 app:tint 替换每个 android:tint。这为我解决了问题。