Android - Setting default style for EditText view is not working

安稳与你 提交于 2019-12-25 04:16:03

问题


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?



来源:https://stackoverflow.com/questions/31359608/android-setting-default-style-for-edittext-view-is-not-working

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