在自定义样式中使用尺寸

Cap*_*orn 7 android android-styles

在Android中,我有以下内容:

dimens.xml

<dimen name="buttonMarginRL">10dp</dimen>
<dimen name="buttonMarginTB">5dp</dimen>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

我将它添加到按钮视图中:

someFragment.xml

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/button"
    style="@style/my_button" />
Run Code Online (Sandbox Code Playgroud)

但利润率没有得到应用.将10dp5dp直接放入my_button样式时,它按预期工作.在Android中是否可以使用dimens.xml中指定的值在自定义样式中使用,或者维度值是否只能直接应用于视图?

Gio*_*ous 0

我知道现在回答已经晚了,但它适合那些正在寻找此类错误的人。对父级的引用是错误的。只需更改 styles.xml 中的以下行

<style name="my_button" parent="@android:style/Widget.Button">
Run Code Online (Sandbox Code Playgroud)

<style name="my_button">
Run Code Online (Sandbox Code Playgroud)

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

<item name="android:buttonStyle">@style/my_button</item>
Run Code Online (Sandbox Code Playgroud)