android : how to change the style of edit text?

2019-01-14 23:11发布

问题:

I'm trying to change the style of the EditText? Is it possible to achieve that? If so I'll appreciate being told about it, otherwise what alternative ways are available.

回答1:

You can use the attribute style="@style/your_style" that is defined for any widget.

To define your style you have to create a file called style.xml in the values folder (i.e. \res\values\styles.xml) and use the following syntax:

<style name="You.EditText.Style" parent="@android:style/Widget.EditText">
    <item name="android:textColor">@color/your_color</item>
    <item name="android:gravity">center</item>
</style>

The attribute parent="@android:style/Widget.EditText" is important because it will ensure that the style being defined extends the basic Android EditText style, thus only properties different from the default style need to be defined.



回答2:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="android:colorBackground">@color/windowBackground</item>
    <item name="android:textColor">@color/textColor</item>
    <item name="colorAccent">@color/colorAccent</item>
    //apply Button style
    <item name="android:editTextStyle">@style/editTextStyle</item>
</style>

//Button Style
<style name="editTextStyle" parent="@style/Widget.AppCompat.EditText">
    <item name="android:textColor">@color/colorWhite</item>
    <item name="android:background">@color/colorPrimary</item>
    <item name="android:textSize">24sp</item>
    <item name="android:paddingTop">10dp</item>
    <item name="android:paddingBottom">10dp</item>
</style>


回答3:

Yes, it is definitely possible. See http://developer.android.com/guide/topics/ui/themes.html (Raghu already postet that link) for more details.

You can also but a background in it, see this post: http://www.anddev.org/tutorial_buttons_with_niceley_stretched_background-t4369.html It only covers buttons, but it is exactly the same for edittexts.