Android 为所有 EditText 设置一致的样式(例如)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10903647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set a consistent style to all EditText (for e.g.)
提问by Code Poet
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:
我试图使EditText
我的应用程序中的所有's 具有一致的外观。我知道我可以做这样的事情:
<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
通过这样做使特定的风格具有这种风格:
<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.
但是这样我必须记住为EditText
我的应用程序中的每一个单独设置样式,这很乏味,如果不是容易出错的话。
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:
有什么办法可以让它成为主题的一部分吗?这样我就不必将此样式与每个EditText
. 类似于这个虚构的代码块:
<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:
然后在我的AndroidManifest.xml
我有类似的东西:
<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.
瞧!我所有EditText
的都具有一致的样式,而不必为每个实例指定样式。
回答by Luksprog
Override the attribute pointing to the EditText
style(named editTextStyle
:) ) in your custom theme:
在您的自定义主题中覆盖指向EditText
style(named editTextStyle
:) )的属性:
<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
:
并使您的自定义样式扩展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:
如果您使用更新的 AppCompat 相关主题,请使用不带 android 前缀的 editTextStyle 属性:
<style name="App_Theme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="editTextStyle">@style/App_EditTextStyle</item>
</style>
回答by iflp
@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.
@Luksprog 答案是正确的,但对我不起作用。经过一些实验,我发现从 editTextStyle 中删除 android 命名空间使它对我有用。
<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
:
并使您的自定义样式扩展Widget.EditText
或使用 AppCompat 主题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>
回答by Reynaldo Aguilar
First, define the style for your EditText. Make sure that the parent style is android:Widget.EditText
首先,定义 EditText 的样式。确保父样式是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).
之后,覆盖android:editTextStyle
自定义主题中的属性。请注意,如果您使用支持库,您还需要覆盖该属性editTextStyle
(没有 android 命名空间)。
<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>
回答by Phil Blandford
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
如果你只需要设置一些简单的参数,比如文本颜色,Android 命名空间有一些参数可以做到这一点,而无需为编辑文本声明单独的样式。例如
<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>
回答by user1700099
<style name="App_Theme" parent="@android:style/Theme.Holo">
<item name="android:editTextBackground">@drawable/filled_roundededges_box_dark</item>
</style>