Android - Setting default style for EditText view

2019-08-30 04:54发布

问题:

I am trying to set default style for edit text view. In this example, I have set default style for editText as well as for textView.

Here is my styles.xml from values folder:

<resources>
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light">
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <item name="android:editTextStyle">@style/AppTheme.EditText</item>
        <item name="android:textViewStyle">@style/AppTheme.TextView</item>
    </style>

    <style name="AppTheme.EditText" parent="android:Widget.EditText">
        <item name="android:textColor">@color/red</item>
        <item name="android:padding">10dp</item>
    </style>

    <style name="AppTheme.TextView" parent="android:Widget.TextView">
        <item name="android:textColor">@color/red</item>
        <item name="android:padding">10dp</item>
    </style>
</resources>

As shown in the snapshots below, I have added two editText and two textView in layout.

Here is my layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="TextView1" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="20dp"
        android:text="TextView2" />

    <EditText
        android:id="@+id/editText1"
        style="@style/AppTheme.EditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="20dp"
        android:ems="10"
        android:text="EditText1" >
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="20dp"
        android:ems="10"
        android:text="EditText2" >
    </EditText>

</RelativeLayout>

Now the issue is, default style for textView works fine but for editText view it does not reflect the style. If I explicitly set style for editText then it reflects as expected (as done for editText1), but not by default. Text color for editText2 should be red but it's not. Am I missing something?

回答1:

Hi prashant..just i copied your code in my project. for me its working

do you want like this? or anything else?