如何使用选择器为ImageView着色? [英] How to use selector to tint ImageView?
问题描述
我想使用XML着色tabhost的图标,而不是通过编程的方式来着色(无论如何我都无法做到)...
I want to tint my tabhost's icons using XML, instead of doing it programmatically (I wasn't able to do that anyway)...
我发现了这一点SO上的线程: Android imageview更改色调以模拟按钮单击
I found this thread on SO: Android imageview change tint to simulate button click
这似乎是一个很好的解决方案,但是我无法在我的项目中正确调整它……我做了以下更改:
That seems to be a pretty good solution, but I wasn't able to adapt it correctly in my project... I did the following changes:
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);
}
}
我也无法在 android:tint
处引用 @ drawable / selector.xml
,所以我在colors.xml:
I also wasn't able to reference @drawable/selector.xml
at android:tint
, so I did this at 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>
有什么建议吗?预先感谢
Any suggestions? Thanks in advance
我收到了 NumberFormatException
使用 android:tint
时,正确的是 app:tint
(设置后) xmlns:app = http://schemas.android.com/apk/res/com.myapp
)...但是现在我认为我以错误的方式使用了选择器,因为图标无论状态如何均为黑色...
我尝试设置< drawable name = tab_icon_selector> @ drawable / tab_icon_selector< / drawable> 无效
I was getting a NumberFormatException
for using android:tint
, when the correct was app:tint
(after setting xmlns:app="http://schemas.android.com/apk/res/com.myapp"
)... but now I think I'm using my selector in a wrong way, because the icons are all black, no matter the state...
I've tried setting <drawable name="tab_icon_selector">@drawable/tab_icon_selector</drawable>
from within colors.xml, didn't work
推荐答案
在 https://stackoverflow.com/a/18724834/2136792 ,您缺少了一些东西:
In reference to my solution at https://stackoverflow.com/a/18724834/2136792, there are a few things you're missing:
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()必须重写,以便在元素状态时更新色调更改。
drawableStateChanged() must be overridden for the tint to be updated when the element's state changes.
我不确定从可绘制对象中引用可绘制对象是否会引起问题,但是您可以简单地将selector.xml移至文件夹 / res /颜色以使用 @ color / selector.xml进行引用(aapt合并了/res/values/colors.xml和/ res / color文件夹)。
I'm not sure if referencing a drawable from a drawable might cause an issue, but you can simply move your selector.xml into a folder "/res/color" to reference it with "@color/selector.xml" (aapt merges both /res/values/colors.xml and the /res/color folder).
这篇关于如何使用选择器为ImageView着色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!