使用appcompat v7自定义EditText样式

3
我创建了一个扩展EditText的自定义视图,并指定了一个属性样式来改变背景色调。
public class CustomEditText extends EditText {

    public CustomEditText (Context context) {
        this(context, null);
    }

    public CustomEditText (Context context, AttributeSet attrs) {
        this(context, attrs, R.attr.customEditTextStyle);
    }

    public CustomEditText (Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs,  R.attr.customEditTextStyle);
    }
    // Some other code...
}

然后我添加了一个style属性:

<resources>
    <attr name="customEditTextStyle" format="reference" />
<resources>

我在我的应用程序中使用Theme.AppCompat。我已经覆盖了colorPrimary,colorPrimaryDark和colorAccent。

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/blue</item>
    <item name="colorPrimaryDark">@color/blue_dark</item>
    <item name="colorAccent">@color/blue_accent</item>
    <item name="customEditTextStyle">@style/CustomEditText</item>
</style>
<style name="CustomEditText" parent="Widget.AppCompat.EditText">
    <item name="colorPrimary">@color/blue</item>
    <item name="colorPrimaryDark">@color/blue_dark</item>
    <item name="colorAccent">@color/blue_accent</item>
</style>

编辑框的背景颜色可以正常工作,但是自定义编辑框无法实现相同效果。我尝试使用了这段代码,但它会改变整体状态,导致所有状态使用相同的颜色。(https://dev59.com/Il8d5IYBdhLWcg3wpzr4#28433337)
editText.getBackground().setColorFilter(getResources().getColor(R.color.your_color), PorterDuff.Mode.SRC_ATOP);

有没有办法在自定义视图中应用AppCompat样式?这是AppCompat问题还是我在CustomEditText中做错了什么?任何想法都将不胜感激。谢谢!

2个回答

6

从API 21开始,您必须子类化AppCompatEditText。所有支持的小部件也是如此。使用AppCompat*获取着色和其他后向兼容功能。


0
通过扩展android.support.v7.internal.widget.TintEditText解决了问题。
AppCompat不支持自定义视图的小部件着色。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接