Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

android - Set a consistent style to all EditText (for e.g.)

I'm trying to make all EditText's in my application have a consistent look. I'm aware that I can do something like this:

<style name="App_EditTextStyle">
    <item name="android:background">@drawable/filled_roundededges_box_dark</item>
    <item name="android:textColor">#808080</item>
    <item name="android:layout_height">45dip</item>
</style>

Then I can make a particular EditText have this style by doing this:

<EditText ...
    style="@style/App_EditTextStyle
    ...>

But this way I have to remember to set the style individually for each and every EditText in my application which is tedious, if not error prone.

Is there some way I could make this a part of a theme or something? This is so I don't have to associate this style to every EditText. Something like this fictitious code block:

<style name="App_Theme" parent="@android:style/Theme.Holo">
   ... 
   <item name="android:EditTextSyle">@style/App_EditTextStyle</item>
   ...
<style>

And then in my AndroidManifest.xml I have something like:

<application
    ....
    android:theme="@style/App_Theme">

And Voila! all my EditText's have the consistent style without me having to specify the style for each instance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Override the attribute pointing to the EditText style(named editTextStyle :) ) in your custom theme:

<style name="App_Theme" parent="@android:style/Theme.Holo">
   <item name="android:editTextStyle">@style/App_EditTextStyle</item>
</style>

and make your custom style to extend Widget.EditText:

<style name="App_EditTextStyle" parent="@android:style/Widget.EditText">
    <item name="android:background">@drawable/filled_roundededges_box_dark</item>
    <item name="android:textColor">#808080</item>
    <item name="android:layout_height">45dip</item>
</style>

Edit:

If you're using the much newer AppCompat related themes use the editTextStyle attribute without the android prefix:

<style name="App_Theme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="editTextStyle">@style/App_EditTextStyle</item>
</style>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...