【问题标题】:Drawable tinting for api <21api <21 的可绘制着色
【发布时间】:2015-03-19 21:37:15
【问题描述】:

是否可以为 api

<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/ic_calendar"
    android:tint="@color/primary" />

工作得很好,但仅适用于具有 API21 的设备。低 api 设备或 AppCompat 支持的任何解决方法?什么都找不到。

【问题讨论】:

    标签: android


    【解决方案1】:

    像这样使用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"
        />
    

    确保您在应用的build.gradle 中有最新的appcompat-v7

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

    【讨论】:

    • From AppCompatImageView docs: This will automatically be used when you use ImageView in your layouts. You should only need to manually use this class when writing custom views. developer.android.com/reference/android/support/v7/widget/… 所以,在布局中使用普通的ImageView 应该可以正常工作。
    • 正如上面提到的@NimrodDayan,这应该没有必要。但是,我收到有关 android:tint 无法在三星 A5 和 Moto G 上运行的报告(使用 appcompat-v7:23.4.0),因此可能在某些设备上无法正确替换 ImageView。
    • @StephenKidson,我使用的是相同版本的 appcompat,并且在未知品牌的设备上也遇到了同样的问题。你设法解决了这个问题吗?我想知道是否有关于此的错误报告......
    • 这在使用 appcompat-v7:25.1.0 的模拟器 Android 4.0 上不起作用。
    • AppCompatImageView 不能在 Widget 中使用。在 ImageView 上使用 setColorFilter。
    【解决方案2】:

    您可以使用源代码来实现。 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));
    

    【讨论】:

    • 如果您需要在 ContextCompat.getColor() 而不是 getResources().getColor()
    【解决方案3】:

    您不能简单地使用 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"
        />
    

    【讨论】:

    • 我正在使用 ImageView - 在其中显示图标。这些图标是我的导航抽屉中元素的一部分。并且导航抽屉中的选定项目具有不同的颜色,因此我创建了每个着色的图标以及每个图标的选择器。我正在使用该选择器作为我的图标。选择器:&lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:state_activated="true" android:drawable="@drawable/ic_home_tinted" /&gt; &lt;item android:drawable="@drawable/ic_home" /&gt; &lt;/selector&gt;
    • @Orochi 看看我的答案,因为它直接来自 Google 的博客。它主要仅适用于 Android 5.0+,但可能适用于运行 Android 5.0 之前的设备的某些小部件。
    • @Orochi 您将不得不制作自定义视图以“模拟”相同的效果。
    • 您可以使用图像视图,使图标尽可能白,并使用 iv.setColorFilter(yourColor, Mode.Multiply); 将其设置为您想要的任何颜色;确保导入 android.graphics.PorterDuff.Mode
    • 这里有同样的问题。遗憾的是,带有选择器的色调不适用于 api
    【解决方案4】:

    之前在这里问过一个类似的问题:https://stackoverflow.com/a/26533340/950427

    仅 Android 5.0+ (API 21+) 支持 Android Drawable Tinting。 (它确实说“At the moment this is limited to coloring the action bar and some widgets.”)。

    主题

    ...

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

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

    小部件着色

    在搭载 Android 5.0 的设备上运行时,所有 使用我们刚刚谈到的颜色主题属性对小部件进行着色 关于。 有两个主要功能可以在 Lollipop 上实现这一点: 可绘制着色和引用主题属性(形式为 ?attr/foo) 在drawables中。

    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/

    【讨论】:

    • 为什么这被否决了?这是来自官方文档。
    • 答案已过时。 ImageView 现在也通过 AppCompat 支持 android:tint,如 @Jonik 的回答。
    • @VickyChijwani 提交编辑。你的意思是AppCompatImageView,而不是ImageView
    【解决方案5】:

    现在 AppCompatImageView,AppCompatButton 将替换 ImageView,Button 以支持 API 较低的设备上的色调。查看链接了解更多详情 AppCompatImageView,AppCompatButton

    【讨论】:

      【解决方案6】:

      对于图像着色,您可以使用imageView.setColorFilter(int color)。这适用于 API 8,用于将我的黑色图像着色为我想要的颜色。 这可以替换setImageTintList(),但仅使用android:tint 也应该可以。

      【讨论】:

        【解决方案7】:

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

        之后,您可以将每个 android:tint 替换为 app:tint。这修复了 给我的问题。

        【讨论】:

          【解决方案8】:

          我有点晚了,但这里是如何做到的。

          val textInput = EditText(context)
          
          val drawable = ContextCompat.getDrawable(context, R.drawable.your_drawable)
          drawable?.let {
              myDrawable -> DrawableCompat.setTint(myDrawable, ContextCompat.getColor(context, R.color.your_color))
              textInput.setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, myDrawable, null)
          }
          

          【讨论】:

            【解决方案9】:

            这将按照您的意愿进行,并且应该适用于支持库的所有 Android 版本:

            @JvmStatic
            fun getTintedDrawable(inputDrawable: Drawable, @ColorInt color: Int): Drawable {
                val wrapDrawable = DrawableCompat.wrap(inputDrawable.mutate())
                DrawableCompat.setTint(wrapDrawable, color)
                DrawableCompat.setTintMode(wrapDrawable, Mode.SRC_IN)
                return wrapDrawable
            }
            

            【讨论】:

              【解决方案10】:

              如果有人想创建新的drawable(tin1,tint2..)试试这个

              <?xml version="1.0" encoding="utf-8"?>
              <bitmap
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:src="@drawable/your_image"
                android:tint="@color/tint_color">
                 </bitmap>
              

              【讨论】:

                猜你喜欢
                • 2015-09-25
                • 2019-05-07
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多