如何使用选择器来着色的ImageView在安卓 [英] How to use selector to tint imageview in android
问题描述
我想用做编程的我tabhost的XML图标,而不是色调(我没能做到这一点呢)......所以,我发现这个线程SO:<一href="http://stackoverflow.com/questions/11095222/android-imageview-change-tint-to-simulate-button-click">Android ImageView的色彩变化来模拟按钮点击
I want to tint my tabhost's icons using xml, instead of doing it programatically (I wasn't able to do that anyway)... So I found this thread on SO: Android imageview change tint to simulate button click
这似乎是一个pretty的很好的解决方案,但我不能在我的项目正确地适应它。我做了以下变化:
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);
}
}
我也没能引用 @绘制/ 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>
我的标签布局:
My tab layout:
<?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版本:色调,当正确的是应用程序:色彩(设置后的xmlns:程序=http://schemas.android.com/apk/res/com.myapp )......但现在我觉得我用我选择了错误的方式,因为图标都是黑色的,无论国家... 我试图从内部colors.xml设置@可绘制/ tab_icon_selector,没有工作
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/tab_icon_selector from within colors.xml, didn't work
[/编辑]
推荐答案
在引用我的解决方案在 http://stackoverflow.com /一/2136792分之18724834,有你就错过了一些东西:
In reference to my solution at http://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 /颜色来引用它与@色/ selector.xml (AAPT合并双方/res/values/colors.xml和/ RES /彩色文件夹)。
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屋!