为所有 EditText 设置一致的样式(例如)

我试图使所有的 EditText在我的应用程序有一个一致的外观。我知道我可以这样做:

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

然后我可以让一个特定的 EditText有这样的风格,通过这样做:

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

但是这样我必须记住为我的应用程序中的每个 EditText单独设置样式,如果不是容易出错的话,这是很乏味的。

我能不能把这个作为主题的一部分?这样我就不必将这种风格与每个 EditText联系起来。比如这个虚构的代码块:

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

然后在我的 AndroidManifest.xml里有这样的东西:

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

瞧! 我所有的 EditText都有一致的风格,而不需要我为每个实例指定风格。

84950 次浏览

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>
<style name="App_Theme" parent="@android:style/Theme.Holo">
<item name="android:editTextBackground">@drawable/filled_roundededges_box_dark</item>
</style>

@Luksprog answer is correct but not working for me. After some experimentation, I found out that removing the android namespace from editTextStyle made it work for me.

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

and make your custom style to extend Widget.EditText or if using the AppCompat theme Widget.AppCompat.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>

First, define the style for your EditText. Make sure that the parent style is android:Widget.EditText

<style name="CustomEditTextStyle" parent="android:Widget.EditText">


<item name="android:textColor">#0F0F0F</item>
<!-- ... More items here if needed ... -->
</style>

After that, override the attribute android:editTextStyle in your custom theme. Be aware, if you are using the support library, you will also need to override the attribute editTextStyle (without the android namespace).

<style name="App_Theme" parent="...">
<item name="android:editTextStyle">@style/CustomEditTextStyle</item>
<item name="editTextStyle">@style/CustomEditTextStyle</item> <!-- For compatibility with the support library -->
</style>

If you just need to set a few simple parameters, like text color, the Android namespace has a few parameters that will do that without the need to declare a separate style for the edit text. Eg

<style name="MyStyle" parent="android:Theme.Material.NoActionBar">
<item name="colorPrimary">@color/black</item>
<item name="colorPrimaryDark">@color/white</item>
<item name="colorAccent">@color/white</item>
<item name="android:textColor">@color/black</item>
<item name="android:editTextColor">@color/black</item>
<item name="android:editTextBackground">@color/black</item>
....
</style>