7

In Android I've got the following:

dimens.xml

<dimen name="buttonMarginRL">10dp</dimen>
<dimen name="buttonMarginTB">5dp</dimen>

style.xml

<style name="my_button" parent="@android:style/Widget.Button">
    <item name="android:textSize">16sp</item>

    <item name="android:layout_marginLeft">@dimen/buttonMarginRL</item>
    <item name="android:layout_marginRight">@dimen/buttonMarginRL</item>
    <item name="android:layout_marginTop">@dimen/buttonMarginTB</item>
    <item name="android:layout_marginBottom">@dimen/buttonMarginTB</item>
</style>

And I'm adding it to a button view:

someFragment.xml

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/button"
    style="@style/my_button" />

But the margins do not get applied. When putting 10dp and 5dp directly into the my_button style it works as expected. Is it possible within Android to use values specified in dimens.xml to be used in custom styles or may the dimens-values only be applied to view's directly?

4

1 回答 1

0

我知道答案已经晚了,但它适用于正在搜索这种错误的人。对父级的引用是错误的。只需从 styles.xml 更改以下行

<style name="my_button" parent="@android:style/Widget.Button">

<style name="my_button">

此外,您可以将 my_button 的所有常用属性添加到样式中的引用中,然后将以下行添加到主题的样式中。通过这样做,您可以跳过活动 xml 中的引用android:buttonStyle

<item name="android:buttonStyle">@style/my_button</item>
于 2017-02-08T11:21:33.567 回答