【问题标题】:Inherit editText from Theme.Light with Holo parent theme从带有 Holo 父主题的 Theme.Light 继承 editText
【发布时间】:2013-06-27 14:56:11
【问题描述】:

我想从android:Theme 继承editText,而我的父主题是android:Theme.Holo.Light
除了将 android sdk 文件夹中的资源复制到我的项目中之外,是否有任何“干净”的方法可以做到这一点?

【问题讨论】:

    标签: android inheritance android-theme


    【解决方案1】:

    所以我的想法是有一个从android:Theme.Holo.Light 扩展的自定义主题(实际上只是一种样式),然后覆盖EditText 属性以使用来自android:Theme 的父设置。

    看起来android:Theme.Holo.Light 使用editTextStyle 属性引用来更改EditTexts 的外观:

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

    因此您可以使用来自android:ThemeeditTextStyle 覆盖它:

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

    然后,您所要做的就是在您的清单中应用YourTheme(如果您真的想的话,也可以在代码中)来获得您的自定义主题。您可能想进一步调整内容,在此处查看所有股票主题:https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/themes.xml

    编辑

    我刚刚对此进行了测试,并意识到android:style/Widget.EditText 似乎没有应用旧版本android 的背景,很可能是因为它使用了android:editTextBackground 的引用,Holo.Light 设置了该引用。我刚刚也尝试手动将android:editTextBackground 更改为旧版EditText drawable,它对我有用。

    注意:仅在/values-v11 下的styles.xml 上应用此调整,因为只有3.0 版并且可以使用android:editTextBackground,并且旧版本的android 已经使用旧的EditText 背景。如果应用最小值 >= API 11,则无需担心。

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

    【讨论】:

    • 这看起来像我要找的东西!我应该写&lt;item name="android:editTextStyle"&gt;@android:style/Widget.EditText&lt;/item&gt;吗?只是“editTextStyle”给了我一个错误,说找不到属性。
    • @beenjaminnn 是的,你是正确的,我忘了添加命名空间,否则 Android 会在你的应用程序中查找该属性(而不是找到它)。请参阅我的更新,因为我的原始答案并没有完全解决问题。
    • 完美,这解决了我的问题!我讨厌全息和材料编辑文本
    【解决方案2】:

    是的,您创建了一个 styles.xml 并将样式父级设置为您想要的任何内容。 :-)

    如何定义样式的示例:

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

    像这样使用这种风格:

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

    在此处了解更多信息:

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

    【讨论】:

    • 感谢您的快速回复,我知道我可以通过这种方式创建自己的自定义editText 样式,但我只想继承旧的android 主题的editText。我会做这样的事情吗? &lt;style name="editTextStyle" parent="@android:Theme.Light:style/editTextStyle"&gt; &lt;/style&gt;
    猜你喜欢
    • 2012-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-11
    相关资源
    最近更新 更多