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


84

我正在尝试使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的样式都保持一致,而无需为每个实例指定样式。

Answers:


155

覆盖自定义主题中指向EditText样式(名为editTextStyle:)的属性:

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

并使您的自定义样式扩展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>

编辑:

如果您使用的是与AppCompat相关的更新主题,请使用没有android前缀的editTextStyle属性:

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

3
这听起来似乎合理且合乎逻辑,但对我来说似乎并不起作用。我会丢失什么吗?
法典诗人

1
@ManishGupta到底怎么不起作用?我已经测试了上面的样式,它们可以工作,但是layout_height/height不能放在那里,因为android可能会在主题样式之前检查那些属性,并且会抱怨它们丢失。但是自定义可绘制内容+不同的文本颜色可与上述方法一起使用。
用户

我已经按照您的描述进行了设置。我重新发布毫无意义。我还是把这个给你。我在某处缺少东西。您规定的是冷逻辑,应该可以工作。待冷静后再重温一下。谢谢。
法典诗人2012年

3
@ toobsco42edittextStyle不适用于AutoCompleTextView(尽管是EditText)。看一下autoCompleteTextViewStyletheme.xml中的属性,该属性指向Widget.AutoCompleTextViewstyle.xml文件中的样式。
用户

3
也不适合我。但是,如果我更改<item name="android:editTextStyle"><item name="editTextStyle">,就可以了。谢谢!
山姆Sch

52

@Luksprog的答案是正确的,但不适用于我。经过一些试验,我发现从editTextStyle删除android名称空间使其对我有用。

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

并让您的自定义样式扩展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>

1
也为我工作。但是为什么它表现得如此?
Eagle_Eye '16

3
可能与AppCompat库有关。您的活动是否持续AppCompatActivity?哦,Android开发的奥秘。
马特·祖科夫斯基

1
@MattZukowski根据您的评论进行了一些挖掘-可能的罪魁祸首:从那时AppCompat v7:21+起,就需要使用标准主题属性,且样式中不应使用“ android:”前缀。
iflp

感谢kkss。
亚当·瓦赫吉

即使不使用Widget.AppCompat.EditText也可以正常工作。但是我无法访问Widget.AppCompat.EditText。说“无法解析符号'@android:style / Widget.AppCompat.EditText'”
Neri

18

首先,为您的EditText定义样式。确保父样式为android:Widget.EditText

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

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

之后,覆盖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>

1

如果您只需要设置一些简单的参数(例如文本颜色),则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>

对于较新版本的AppCompat,请使用editTextColor并删除android:
阿明

-2
<style name="App_Theme" parent="@android:style/Theme.Holo">    
    <item name="android:editTextBackground">@drawable/filled_roundededges_box_dark</item>
</style>
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.