在Android XML中,我可以将ButtonStyleSmall或Button.Small作为父样式设置为自定义样式吗? [英] In Android XML, can I set buttonStyleSmall or Button.Small as a parent style to a custom style?

查看:56
本文介绍了在Android XML中,我可以将ButtonStyleSmall或Button.Small作为父样式设置为自定义样式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想扩展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"/>

但出于可重用性的考虑,我希望将这些样式转换为自定义样式。如何添加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>

使用该样式的按钮声明:

<Button android:id="@+id/myButton"
        style="@style/RightLink"
        android:text="Click Here"/>

编辑

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

添加了ButtonStyleSmall样式和内联样式的按钮:

以ButtonStyleSmall为父级的自定义样式:

我错过了什么?

推荐答案

所以您尝试inherit style informations来自标准的Android风格。为此,您需要像以前一样使用parent-属性。

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

<style name="RightLink" parent="@android:attr/buttonStyleSmall">

有关如何对平台样式执行此操作的更多信息(因为这与您自己创建的样式不同)here


玩了一下,我发现你入侵的方式是正确的,但来源是错误的:

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

这行得通。

不同之处在于,使用style-属性和代码将其相加所使用的整数常量是在attr-子类的andidsR-类中声明的。

XML样式的定义(可以实际继承)存储在R-类的style-子类中。因此,上面这行应该可以解决您的问题。

这篇关于在Android XML中,我可以将ButtonStyleSmall或Button.Small作为父样式设置为自定义样式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆