【问题标题】:Programmatically change backgroundTint of ImageView with Vector Asset for background使用 Vector Asset 以编程方式更改 ImageView 的 backgroundTint 作为背景
【发布时间】:2017-08-22 19:32:44
【问题描述】:
<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:backgroundTint="@color/colorRed"
    android:background="@drawable/ic_delete"/>

如上所述,我为 ImageView 背景添加了一个 Android VectorAsset。
我可以通过如下的 xml 将 Vector Asset 的颜色从红色更改为蓝色。

android:backgroundTint="@color/colorBlue"

但我想以编程方式更改其颜色。

【问题讨论】:

    标签: android android-layout imageview android-xml


    【解决方案1】:

    您可以使用AppCompatImageView,而不是使用ImageView,因为API 级别21 支持setBackgroundTintList,如果您使用AppCompatImageView,您可以使用setSupportBackgroundTintList 更改色调颜色。

    所以像这样改变你的 ImageView,

    <android.support.v7.widget.AppCompatImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/colorRed"
        android:background="@drawable/ic_delete"/>
    

    这样你就可以像这样调用setSupportBackgroundTintList来设置色调颜色,

    imageView.setSupportBackgroundTintList(ContextCompat.getColorStateList(this, R.color.colorBlue));
    

    【讨论】:

    • 我可以在ImageView 上进行操作,但我的应用至少支持v21
    【解决方案2】:

    您可以通过编程方式实现它:

    img.setColorFilter(getResources().getColor(R.color.white));
    

    【讨论】:

      【解决方案3】:

      首先 background 和 src 是 ImageView 的不同属性。 您是否尝试将图像设置为 ImageView?首先你要使用属性: android:src="drawable"

      如果您使用矢量资产,则需要使用属性:app:srcCompat="drawable"

      要了解背景,请阅读 backgroundTint 属性:What is the difference between background, backgroundTint, backgroundTintMode attributes in android layout xml?

      如果这是您真正想要的,最后以编程方式回答您对背景色调的问题,那么这里有一个链接How to add button tint programmatically

      希望对你有帮助!

      【讨论】:

        【解决方案4】:

        This 问题是相关的,但是按钮的答案。

        user A.A的原回答


        你应该使用setBackgroundTintList(ColorStateList list)

        关注link 了解如何创建颜色状态列表资源。

        <?xml version="1.0" encoding="utf-8"?>
        <selector xmlns:android="http://schemas.android.com/apk/res/android" >
            <item
                android:color="#your_color_here" />
        </selector>
        

        然后使用加载它

        setBackgroundTintList(contextInstance.getResources().getColorStateList(R.color.your_xml_name));
        

        其中contextInstanceContext 的一个实例

        使用 AppCompat

        btnTag.setSupportButtonTintList(ContextCompat.getColorStateList(Activity.this, R.color.colorPrimary));
        

        【讨论】:

          【解决方案5】:
              imageView.apply {
                  setColorFilter(ContextCompat.getColor(context, tintColor), android.graphics.PorterDuff.Mode.MULTIPLY)
                  backgroundTintList = ContextCompat.getColorStateList(context, bgTintColor)
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-03-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-05-15
            相关资源
            最近更新 更多