在Android XML中,我可以将buttonStyleSmall或Button.Small设置为自定义样式的父样式吗?

ann*_*nie 9 android android-layout android-xml android-styles

我想扩展Android的小按钮风格.我可以内联:

<Button android:id="@+id/myButton"
        style="?android:attr/buttonStyleSmall"
        android:layout_alignParentRight="true"
        android:layout_gravity="right"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:textSize="10dp"
        android:text="Click Here"/>
Run Code Online (Sandbox Code Playgroud)

但为了可重用性,我想将这些样式转换为自定义样式.如何将buttonStyleSmall(或Widget.Button.Small?)作为父项添加到样式中?在我的自定义样式XML中这样的东西:

<style name="RightLink" parent="?android:attr/buttonStyleSmall">
  <item name="android:layout_alignParentRight">true</item>
  <item name="android:layout_gravity">right</item>
  <item name="android:layout_height">wrap_content</item>
  <item name="android:layout_width">wrap_content</item>
  <item name="android:paddingLeft">2dp</item>
  <item name="android:paddingRight">2dp</item>
  <item name="android:textSize">10dp</item>
</style>
Run Code Online (Sandbox Code Playgroud)

使用该样式的按钮声明:

<Button android:id="@+id/myButton"
        style="@style/RightLink"
        android:text="Click Here"/>
Run Code Online (Sandbox Code Playgroud)

编辑

使用下面Lukas描述的正确语法(@android:attr/buttonStyleSmall用作父母),我仍然看到两者之间的区别:

使用buttonStyleSmall作为样式和内联样式的按钮:

PIC1

使用buttonStyleSmall作为父级的自定义样式:

PIC2

我错过了什么?

Luk*_*uth 15

因此,您尝试从标准Android样式继承样式信息.为此,您需要像以前一样使用parent-attribute.

继承标准样式时唯一的例外是,您必须使用@不是?:

<style name="RightLink" parent="@android:attr/buttonStyleSmall">
Run Code Online (Sandbox Code Playgroud)

有关如何为平台样式执行此操作的更多信息(因为这与您自己创建的样式不同)可以在此处找到.


在玩弄它后,我发现你的方式是正确的,但追索是错误的:

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

这有效.

差异接缝是用于通过使用style-attribute和代码添加它的整数常量Rattr-subclass 中的Androids -class中声明.

XML样式定义(实际上可以从中继承)存储在-class 的style-subclass中R.所以上面的行应该可以解决你的问题.

  • 所以我很困惑, &lt;style name="RightLink"parent="@android:attr/buttonStyleSmall"&gt; 仍然是有效的行吗?Android Studio 说不是。 (2认同)
  • 与@DanielOchoa一样,表示这不是有效行 (2认同)