本教程将介绍在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作为父样式设置为自定义样式吗?的教程就到这里就结束了,希望趣模板源码网找到的这篇技术文章能帮助到大家,更多技术教程可以在站内搜索。