2015-04-22 73 views
3

我创建了一个扩展EditText的自定义视图,并指定了一个属性样式以更改背景色调颜色。使用appcompat v7自定义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... 
} 

然后我添加的属性风格:

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

我在我的应用程序使用Theme.AppCompat。我已经压倒了色彩主色调,色彩主色调和色彩渐变。

<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> 

editText背景颜色工作正常,但是,我不能做自定义editText相同。

我已经使用此代码尝试,但它改变了整体的状态,使得所有国家使用相同的颜色。(https://stackoverflow.com/a/28433337

editText.getBackground().setColorFilter(getResources().getColor(R.color.your_color), PorterDuff.Mode.SRC_ATOP); 

是否有自定义视图应用程序兼容性样式的方法吗?这是AppCompat问题还是我在我的CustomEditText中做了错误?任何想法将不胜感激。谢谢!

回答

5

从api 21开始,您必须继承AppCompatEdit文本。所有支持的小部件也是如此。使用AppCompat *获取着色和其他后台支持的功能。

0

通过扩展android.support.v7.internal.widget.TintEditText解决了这个问题。

AppCompat不支持自定义视图的构件着色。