Style inheritance of extended Widget

佐手、 提交于 2019-12-04 08:57:34

Has a way very, very, very easy.

Instead of inheriting from android.widget.EditText your Custom EditText, inherit this class and see if it works:

android.support.v7.internal.widget.TintEditText

Link to the class: TintEditText.java

Read the Javadoc of the class, he says:

/**
 * An tint aware {@link android.widget.EditText}.
 * <p>
 * This will automatically be used when you use {@link android.widget.EditText} in your
 * layouts. You should only need to manually use this class when writing custom views.
 */

In the stretch of this page (Using Support Library APIs) has a caution which is: When using classes from the Support Library, be certain you import the class from the appropriate package. For example, when applying the ActionBar class:

  • android.support.v7.app.ActionBar when using the Support Library.
  • android.app.ActionBar when developing only for API level 11 or higher.

I interpreted this, that is, if you are using a support library, always try to import or inherit the appropriate package. (Nothing less than that is written lol.: D)

Krzysztof

You can work around it using styles

In your Theme

<style name="AppTheme.BaseNoActionBar" parent="Theme.AppCompat.Light">
    <item name="android:editTextStyle">@style/Widget.EditText</item>
</style>


<!-- EditText style -->
<style name="Widget.EditText" parent="Widget.AppCompat.EditText">
    <item name="android:textCursorDrawable">@drawable/cursor_gray</item>
    <item name="android:background">@drawable/underline_gray</item>
</style>

then define the cursor

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <size android:width="1dp" />
    <solid android:color="@color/lightGrayLabel"  />
</shape>

and the drawable according to this hack https://stackoverflow.com/a/20891131

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:top="-6dp" android:left="-6dp" android:right="-6dp">
        <shape android:shape="rectangle">
            <solid android:color="@android:color/transparent"/>
            <stroke android:color="@color/lightGrayLabel" android:width="3dp"/>
        </shape>
    </item>
</layer-list>

I have also tried using selector as the "background" field, but it didn't work with either "android:state_selected" or "android:state_activated"

Here's a little example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!