Inherit editText from Theme.Light with Holo parent theme

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-21 05:02:23

问题


I want to inherit the editText from android:Theme while my parent theme is android:Theme.Holo.Light.
Is there any "clean" way to do this short of copying the resources from the android sdk folder into my project?


回答1:


So my idea would be to have a custom theme (really just a style) that extends from android:Theme.Holo.Light, and then overwrites the EditText attributes to use the parent settings from android:Theme.

It looks like android:Theme.Holo.Light uses an editTextStyle attribute reference to change the appearance of the EditTexts:

<item name="editTextStyle">@android:style/Widget.Holo.Light.EditText</item>

So you can override it with the editTextStyle from android:Theme:

<style name="YourTheme" parent="android:Theme.Holo.Light">
    <item name="android:editTextStyle">@android:style/Widget.EditText</item>
</style>

Then, all you have to do is apply YourTheme in your manifest (or in code if you really want to) to get your custom theme. You may want to tweak stuff further, check out all the stock themes here: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/themes.xml

Edit

I just tested this out and realized that it appears that android:style/Widget.EditText doesn't seem to apply the background from older versions of android, most likely because it uses a reference to android:editTextBackground, which Holo.Light sets. I just tried also changing android:editTextBackground manually to the legacy EditText drawable, and it works for me.

Note: Only apply this adjustment on your styles.xml under /values-v11, since only version 3.0 and can use android:editTextBackground, and older versions of android will already use the old EditText background. If the app min is >= API 11, you don't need to worry about this.

<style name="YourTheme" parent="android:Theme.Holo.Light">
    <item name="android:editTextStyle">@android:style/Widget.EditText</item>
    <item name="android:editTextBackground">@android:drawable/edit_text</item>
</style>



回答2:


Yes you create a styles.xml and set the style parent to whatever you want. :-)

Example of how to define a style:

<?xml version="1.0" encoding="utf-8"?>
    <resources>
        <style name="CodeFont" parent="@android:style/TextAppearance.Medium">
            <item name="android:layout_width">fill_parent</item>
            <item name="android:layout_height">wrap_content</item>
            <item name="android:textColor">#00FF00</item>
            <item name="android:typeface">monospace</item>
        </style>
    </resources>

Use this style like so:

<TextView
    style="@style/CodeFont"
    android:text="@string/hello" />

Read more about it here:

http://developer.android.com/guide/topics/ui/themes.html#DefiningStyles



来源:https://stackoverflow.com/questions/17346550/inherit-edittext-from-theme-light-with-holo-parent-theme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!