gpt4 book ai didi

android - 更改 NinePatchDrawable 资源颜色

转载 作者:搜寻专家 更新时间:2023-11-01 08:47:01 25 4
gpt4 key购买 nike

我正在尝试设置 Spinner 元素的样式。我为此创建了带有图层列表的资源。我在那里使用标准库资源@drawable/abc_spinner_mtrl_am_alpha。它是一个箭头,它始终显示为白色。在android的api版本21中,在spinner中可以设置属性android:backgroundTint,但我不知道在早期版本中如何做到这一点。

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="4dp"
android:left="4dp"
android:right="4dp"
android:top="4dp" >

<shape android:shape="rectangle">
<stroke
android:color="#999"
android:width="1dp" />
<corners android:radius="2dip" />
<solid android:color="@android:color/transparent"/>
</shape>

</item>

<item android:id="@+id/notPressedSpinner"
android:drawable="@drawable/abc_spinner_mtrl_am_alpha"
android:right="4dp" />
</layer-list>

我尝试使用 setColorFilter 以编程方式更改资源,但它不起作用。

    Drawable abcSpinnerMtrlAmAlpha = mContext.getResources()
.getDrawable(R.drawable.abc_spinner_mtrl_am_alpha);
Drawable abcSpinnerMtrlAmAlpha2 = mContext.getResources()
.getDrawable(R.drawable.abc_spinner_mtrl_am_alpha);

abcSpinnerMtrlAmAlpha.setColorFilter(mContext.getResources().getColor(R.color.gray_color),
PorterDuff.Mode.SRC_ATOP);
abcSpinnerMtrlAmAlpha2.setColorFilter(mContext.getResources().getColor(R.color.green),
PorterDuff.Mode.SRC_ATOP);

LayerDrawable x = ((LayerDrawable)mContext.getResources().getDrawable(R.drawable.x));
LayerDrawable y = ((LayerDrawable)mContext.getResources().getDrawable(R.drawable.y));

x.setDrawableByLayerId(R.id.notPressedSpinner, abcSpinnerMtrlAmAlpha);
y.setDrawableByLayerId(R.id.notPressedSpinner, abcSpinnerMtrlAmAlpha2);

最佳答案

我的解决方案

public class StateListDrawableWithTint extends StateListDrawable {
private ColorStateList mColorStateList;
private PorterDuff.Mode mFilterMode;

public StateListDrawableWithTint(Drawable drawable, ColorStateList colorStateList) {
this(drawable, colorStateList, PorterDuff.Mode.MULTIPLY);
}

public StateListDrawableWithTint(Drawable drawable, ColorStateList colorStateList, PorterDuff.Mode filterMode) {
super();

mColorStateList = colorStateList;
mFilterMode = filterMode;

addState(new int[]{}, drawable);
}

@Override
protected boolean onStateChange(int[] states) {
if (mColorStateList != null) {
int stateColor = mColorStateList.getColorForState(states, Color.TRANSPARENT);

super.setColorFilter(stateColor, mFilterMode);
}

return super.onStateChange(states);
}
}

使用:

spinner.setBackgroundDrawable(new StateListDrawableWithTint(
getResources().getDrawable(R.drawable.sum_currency_spinner),
getResources().getColorStateList(R.color.sum_currency_spinner),
PorterDuff.Mode.SRC_ATOP));

可绘制:

        <shape android:shape="rectangle">
<stroke
android:color="#999"
android:width="1.5dp" />
<corners android:radius="2dip" />
<solid android:color="@android:color/transparent"/>
</shape>

</item>

<item android:id="@+id/notPressedSpinner"
android:drawable="@drawable/abc_spinner_mtrl_am_alpha"
android:right="4dp" />
</layer-list>

颜色状态:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- disabled state -->
<item android:state_enabled="false" android:color="@color/light_green"/>
<!-- pressed state -->
<item android:state_enabled="true" android:state_window_focused="true" android:state_pressed="true" android:color="@color/light_green" />
<!-- unselected state -->
<item android:state_enabled="true" android:state_window_focused="true" android:color="@color/gray_color" />
<!-- dropdown list state -->
<item android:state_enabled="true" android:state_focused="true" android:color="@color/light_green" />
<!-- default -->
<item android:color="@color/light_green" />
</selector>

关于android - 更改 NinePatchDrawable 资源颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27303325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com