【问题标题】:Managing Android's hierarchical styles (parenting)管理 Android 的分层样式(育儿)
【发布时间】:2013-05-01 02:50:47
【问题描述】:

您能帮我理解一下我对 Android 样式和主题的概念吗?

这是我的问题:我希望我的按钮按照以下属性运行:style="?android:attr/buttonStyleSmall"

在这种情况下,按钮如下所示:

由于我的背景是黑色的,我觉得我的按钮上的字体颜色为白色以具有更好的对比度。然后我决定在我的 styles.xml 文件中创建一个元素来覆盖 buttonStyleSmall 样式并添加这种白色字体,以及一些其他更改:

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="MyTheme" parent="android:Theme.Holo.Light">
        <item name="android:buttonStyle">@style/ButtonText</item>
    </style>

    <style name="ButtonText" parent="@android:attr/buttonStyleSmall">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">match_parent</item>
        <item name="android:textColor">#ffffff</item>
        <item name="android:gravity">center</item>
        <item name="android:paddingLeft">20dp</item>
        <item name="android:paddingRight">20dp</item>
        <item name="android:paddingTop">10dp</item>
        <item name="android:paddingBottom">10dp</item>
        <item name="android:textSize">14sp</item>
        <item name="android:textStyle">bold</item>
    </style>

</resources>

现在,我创建了一个名为 MyTheme 的主题,我计划将其应用于我的整个应用程序。这样我就不必在每次创建按钮时都声明按钮的样式。所以我只会有这样的按钮声明:

<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

不幸的是,当我这样做时,这是我拥有的按钮:

发生了什么?我确实在我的样式中声明了父属性,那么为什么按钮中的灰色框消失了?它也不像一个按钮(即:当我点击它时,它不会改变颜色)。 如果我这样声明按钮:

 <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/ButtonText"
        android:text="Button" />

同样的问题发生了。

我做错了什么?

谢谢各位, 费利佩

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    我相信你想从@android:style/Widget.Button.Small继承。

    这会将父级指向实际的 XML 样式定义,而 @android:attr/buttonStyleSmall 指向 buttonStyleSmall 的属性而不是样式本身。

    【讨论】:

    • 嗯,这行得通:-) 你能告诉我属性 style="?android:attr/buttonStyleSmall" 是什么意思吗?它是否意味着只能与 XML 内联使用?
    • Styles and Themes 指南的最后一段似乎将属性 (android:attr) 描述为 XML 属性的可用选项,而样式 (android:style) 是实际样式。我相信 android:attr/buttonStyleSmall 只是样式的参考。
    • 谢谢坦尼斯。非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-06
    • 2021-11-13
    • 1970-01-01
    • 2015-02-25
    相关资源
    最近更新 更多