开发者

android : how to change the style of edit text?

开发者 https://www.devze.com 2023-03-30 10:12 出处:网络
I\'m trying to change the style of the EditText? Is it possible to achieve that? If so I\'ll appreciate being told abou开发者_StackOverflowt it, otherwise what alternative ways are available.You can u

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


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.


<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>


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号