42

我想使用 XML 为我的 tabhost 图标着色,而不是以编程方式进行(无论如何我都无法做到)...

我在 SO 上找到了这个线程:Android imageview change tint to simulation button click

这似乎是一个很好的解决方案,但我无法在我的项目中正确调整它......我做了以下更改:

public class TintableImageView extends ImageView {
    private ColorStateList tint;

    public TintableImageView(Context context) {
        super(context);
    }

    //this is the constructor that causes the exception
    public TintableImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public TintableImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs, defStyle);
    }

    //here, obtainStyledAttributes was asking for an array
    private void init(Context context, AttributeSet attrs, int defStyle) {
        TypedArray a = context.obtainStyledAttributes(attrs, new int[]{R.styleable.TintableImageView_tint}, defStyle, 0);
        tint = a.getColorStateList(R.styleable.TintableImageView_tint);
        a.recycle();
    }

    @Override
    protected void drawableStateChanged() {
        super.drawableStateChanged();
        if (tint != null && tint.isStateful())
            updateTintColor();
    }

    public void setColorFilter(ColorStateList tint) {
        this.tint = tint;
        super.setColorFilter(tint.getColorForState(getDrawableState(), 0));
    }

    private void updateTintColor() {
        int color = tint.getColorForState(getDrawableState(), 0);
        setColorFilter(color);
    }

}

我也无法参考@drawable/selector.xmlandroid:tint所以我在 colors.xml 中做了这个:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="azulPadrao">#2e7cb4</color>
<drawable name="tab_icon_selector">@drawable/tab_icon_selector</drawable>
</resources>

我的选择器:

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:tint="#007AFF" />
<item android:state_focused="true" android:tint="#007AFF" />
<item android:state_pressed="true" android:tint="#007AFF" />
<item android:tint="#929292" />
</selector>

我的标签布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical" android:id="@+id/TabLayout"
          android:layout_width="fill_parent" android:layout_height="fill_parent"
          android:gravity="center" android:background="@drawable/tab_bg_selector">

<com.myapp.TintableImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView" android:layout_gravity="center" android:tint="@drawable/tab_icon_selector"/>
<TextView android:id="@+id/TabTextView" android:text="Text"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" android:textColor="@drawable/tab_text_selector"
          android:textSize="10dip"
          android:textStyle="bold" android:layout_marginTop="2dip"/>

</LinearLayout>

有什么建议么?提前致谢

编辑

我得到了一个NumberFormatExceptionfor using android:tint,当正确的是app:tint(在设置之后xmlns:app="http://schemas.android.com/apk/res/com.myapp")......但现在我认为我以错误的方式使用我的选择器,因为图标都是黑色的,无论状态如何......我已经尝试<drawable name="tab_icon_selector">@drawable/tab_icon_selector</drawable>从 colors.xml 中设置,没有用

4

6 回答 6

42

如果您使用的是 API 21+,则可以使用选择器和tint在 XML 中轻松完成此操作:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_activated="true">
        <bitmap android:src="@drawable/ic_settings_grey"
                android:tint="@color/primary" />
    </item>

    <item android:drawable="@drawable/ic_settings_grey"/>
</selector>
于 2015-02-05T20:58:04.077 回答
22

我使用DrawableCompatAndroid support-v4 库实现了这一点。

使用常规ImageButton(子类ImageView,因此此信息也适用于ImageViews),使用材料图标集合中的黑色图标:

<ImageButton
  android:id="@+id/button_add"
  android:src="@drawable/ic_add_black_36dp"
  android:background="?attr/selectableItemBackgroundBorderless"
  android:contentDescription="@string/title_add_item" />

这是我创建的实用方法:

public static void tintButton(@NonNull ImageButton button) {
    ColorStateList colours = button.getResources()
            .getColorStateList(R.color.button_colour);
    Drawable d = DrawableCompat.wrap(button.getDrawable());
    DrawableCompat.setTintList(d, colours);
    button.setImageDrawable(d);
}

res/color/button_colour.xml按下按钮时将图标颜色从红色变为半透明红色的选择器在哪里:

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

    <item
      android:state_pressed="false"
      android:color="@color/red" />

    <item
      android:color="@color/red_alpha_50pc" />

</selector>

ImageButton我的活动onCreate()方法中膨胀之后,我只为每个按钮调用tintButton(...)一次帮助方法。


我已经在 Android 4.1(我的minSdkVersion)和 5.0 设备上对此进行了测试,但DrawableCompat应该可以回到 Android 1.6。

于 2015-08-05T15:10:25.530 回答
12

参考我在https://stackoverflow.com/a/18724834/2136792的解决方案,您缺少一些东西:

TintableImageView.java

@Override
protected void drawableStateChanged() {
    super.drawableStateChanged();
    if (tint != null && tint.isStateful())
        updateTintColor();
}

public void setColorFilter(ColorStateList tint) {
    this.tint = tint;
    super.setColorFilter(tint.getColorForState(getDrawableState(), 0));
}

private void updateTintColor() {
    int color = tint.getColorForState(getDrawableState(), 0);
    setColorFilter(color);
}

必须覆盖 drawableStateChanged() 才能在元素状态更改时更新色调。

我不确定从drawable引用drawable是否会导致问题,但您可以简单地将您的selector.xml移动到文件夹“/res/color”以使用“@color/selector.xml”引用它(aapt合并/res/values/colors.xml 和 /res/color 文件夹)。

于 2013-10-22T20:32:45.480 回答
11

借助支持库 22.1,我们可以使用 DrawableCompat 为可绘制对象着色,API 级别 4+

DrawableCompat.wrap(Drawable) 和 setTint()、setTintList() 和 setTintMode() 将正常工作:无需创建和维护单独的可绘制对象,仅支持多种颜色!

于 2015-04-22T06:59:07.443 回答
11

我同意代码中的@Dreaming,我将举一个例子。

ic_up_small

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:color="@color/comment_count_selected_color" android:state_selected="true" />
    <item android:color="@color/comment_count_text_color"/>

</selector>

布局/item_post_count_info.xml

<android.support.v7.widget.AppCompatImageView
    android:id="@+id/post_upvote_icon"
    android:layout_width="14dp"
    android:layout_height="14dp"
    android:layout_marginLeft="17dp"
    app:srcCompat="@drawable/ic_up_small"
    app:tint="@color/post_up_color"/>

注意:我们应该使用app:tint而不是android:tint

我的支持库版本是 26.0.2。

应用程序/build.gradle

implementation 'com.android.support:appcompat-v7:26.0.2'
implementation 'com.android.support:support-core-utils:26.0.2'
implementation 'com.android.support:support-annotations:26.0.2'
implementation 'com.android.support:support-v4:26.0.2'
implementation 'com.android.support:design:26.0.2'

如果我们使用android:tint,它会崩溃,日志是这样的:

E/AndroidRuntime:致命异常:主 android.view.InflateException:二进制 XML 文件第 0 行:在 android.view.LayoutInflater.createViewFromTag(LayoutInflater.createViewFromTag(LayoutInflater. java:687) 在 android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 在 android.view.LayoutInflater.inflate(LayoutInflater.java:489) 在 android.view.LayoutInflater。在 com.opera.six.viewholder.post.PostCountInfoViewHolder$1.create(PostCountInfoViewHolder.java:29) 在 com.opera.six.viewholder.post.PostCountInfoViewHolder$1.create(PostCountInfoViewHolder.java: 25) 在 com.opera.six.collection.CollectionAdapter.onCreateViewHolder(CollectionAdapter.java:39) 在 com.opera.six.collection.CollectionAdapter.onCreateViewHolder(CollectionAdapter.java:19) 在 android.support.v7.widget.RecyclerView $适配器。createViewHolder(RecyclerView.java:6493) at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5680) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5563)在 android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5559) 在 android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2229) 在 android.support.v7.widget .LinearLayoutManager。layoutChunk(LinearLayoutManager.java:1556) 在 android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1516) 在 android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:608) 在 android.support .v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3693) 在 android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3410) 在 android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java :3962) 在 android.view.View.layout(View.java:13754) 在 android.view.ViewGroup.layout(ViewGroup.java:4364) 在 android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:610 ) 在 android.view.View.layout(View.java:13754) 在 android.view.ViewGroup.layout(ViewGroup.java:4364) 在 android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:132)在 android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42) 在 android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1361) 在 android.support.design.widget .CoordinatorLayout.onLayout(CoordinatorLayout.java:869) 在 android.view.View.layout(View.java:13754) 在 android.view.ViewGroup.layout(ViewGroup.java:4364) 在 android.support.v4.view。ViewPager.onLayout(ViewPager.java:1767) at android.view.View.layout(View.java:13754) at android.view.ViewGroup.layout(ViewGroup.java:4364) at android.widget.LinearLayout.setChildFrame(LinearLayout .java:1649) 在 android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507) 在 android.view.View.layout(View.java:13754) 在 android.view.View.layout(View.java:13754) 在 android.widget.FrameLayout.onLayout(FrameLayout.java:448) 在 android.view.ViewGroup.layout(ViewGroup.java:4364) 在 android. view.ViewGroup.layout(ViewGroup.java:4364) 在 android.view.ViewGroup 的 android.view.View.layout(View.java:13754) 在 android.widget.FrameLayout.onLayout(FrameLayout.java:448)。android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649) 的布局(ViewGroup.java:4364) android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507) 的 android.widget.LinearLayout.onLayout(LinearLayout.java :1420) 在 android.view.View.layout(View.java:13754) 在 android.view.ViewGroup.layout(ViewGroup.java:4364) 在 android.widget.FrameLayout.onLayout(FrameLayout.java:448)在 android.view.View.layout(View.java:13754) 在 android.view.ViewGroup.layout(ViewGroup.java:4364) 在 android.widget.FrameLayout.onLayout(FrameLayout.java:448) 在 android。 view.View.layout(View.java:13754) 在 android.view.ViewGroup.layout(Vi4364) 在 android.view.View.layout(View.java:13754) 在 android.view.ViewGroup.layout(Vi4364) 在 android.view.View.layout(View.java:13754) 在 android.view.ViewGroup.layout(Vi

于 2017-11-15T12:22:24.330 回答
4

With current AppCompat support library, you can use app:tint on ImageView tag which will be inflated as AppCompatImageView and handle the state change properly.

In AppCompatImageView, you can see that mImageHelper is notified of the state change:

@Override
protected void drawableStateChanged() {
    super.drawableStateChanged();
    if (mBackgroundTintHelper != null) {
        mBackgroundTintHelper.applySupportBackgroundTint();
    }
    if (mImageHelper != null) {
        mImageHelper.applySupportImageTint();
    }
}

Android Studio currently gives a warning on this, but you can safely suppress it.

于 2017-06-19T15:17:58.090 回答