简体   繁体   中英

EditText custom style does not display

I'm trying to apply my custom style to EditText, but it's not working.

@drawable/edit_text_background:

    <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <shape android:shape="rectangle"/>
    <solid android:color="@color/background"/>
</shape>

styles.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar" />

    <style name="editText" parent="Widget.AppCompat.EditText">
        <item name="colorControlNormal">@color/black</item>
        <item name="colorControlActivated">@color/yellow_button</item>
        <item name="colorControlHighlight">@color/purple_button</item>
        <item name="background">@drawable/edit_text_background</item>
    </style>
</resources>

themes.xml:

<resources xmlns:tools="http://schemas.android.com/tools">
<style name="CrossyTheme" parent="Theme.AppCompat">
    <item name="editTextStyle">@style/editText</item>
</style>
</resources>

and my EditText in sign_up_activity.xml:

<androidx.appcompat.widget.AppCompatEditText
    android:id="@+id/emailEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="left"
    android:layout_marginLeft="45dp"
    android:layout_marginRight="45dp"
    android:layout_marginTop="50dp"
    android:textColor="@color/black"
    android:hint="@string/email"
    android:textColorHint="@color/hints"
    android:fontFamily="@font/regular"
    android:textColorHighlight="@color/purple_button"
    android:inputType="textEmailAddress"
    style="@style/Widget.AppCompat.EditText"
    />

And no matter what, this EditText widget still has default style! What am I doing wrong?

Yes, you are actually applying the default style here:

<androidx.appcompat.widget.AppCompatEditText
    android:id="@+id/emailEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="left"
    android:layout_marginLeft="45dp"
    android:layout_marginRight="45dp"
    android:layout_marginTop="50dp"
    android:textColor="@color/black"
    android:hint="@string/email"
    android:textColorHint="@color/hints"
    android:fontFamily="@font/regular"
    android:textColorHighlight="@color/purple_button"
    android:inputType="textEmailAddress"
    style="@style/Widget.AppCompat.EditText"
    />

The right thing to do is:

<androidx.appcompat.widget.AppCompatEditText
    android:id="@+id/emailEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="left"
    android:layout_marginLeft="45dp"
    android:layout_marginRight="45dp"
    android:layout_marginTop="50dp"
    android:textColor="@color/black"
    android:hint="@string/email"
    android:textColorHint="@color/hints"
    android:fontFamily="@font/regular"
    android:textColorHighlight="@color/purple_button"
    android:inputType="textEmailAddress"
    style="@style/editText"
    />

Well, I found out that problem was in themes (I didn't make it right). Check your theme in manifest if you have the same problem!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM