您可以试试帮我理解我的Android风格和主题概念吗?管理Android的分层样式(育儿)
这里是我的问题:我希望我的按钮,根据下面的属性行为:style="?android:attr/buttonStyleSmall"
在这种情况下,该按钮看起来是这样的:
因为我的黑色背景,我觉得有我的按钮上有一个白色的字体颜色,以获得更好的对比度。我决定再在我的styles.xml文件中创建一个元素来覆盖buttonStyleSmall
风格,并添加这个白色字体,再加上一些其他的变化:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="MyTheme" parent="android:Theme.Holo.Light">
<item name="android:buttonStyle">@style/ButtonText</item>
</style>
<style name="ButtonText" parent="@android:attr/buttonStyleSmall">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">match_parent</item>
<item name="android:textColor">#ffffff</item>
<item name="android:gravity">center</item>
<item name="android:paddingLeft">20dp</item>
<item name="android:paddingRight">20dp</item>
<item name="android:paddingTop">10dp</item>
<item name="android:paddingBottom">10dp</item>
<item name="android:textSize">14sp</item>
<item name="android:textStyle">bold</item>
</style>
</resources>
现在,我创建了一个名为MyTheme
主题,我打算申请我的整个申请。这样我就不必在每次创建按钮时都声明按钮的样式。所以,我只会有按钮的声明是这样的:
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
不幸的是,当我这样做,这是我按钮有:
发生了什么?我在我的样式中声明了父属性,为什么按钮中的灰色框消失了?它也不像一个按钮(即:当我点击它时,它不会改变颜色)。 如果我宣布这样的按钮:
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/ButtonText"
android:text="Button" />
的同样的问题发生。
我在做什么错?
谢谢你们, 费利佩
那么,那个作品:-) 你是不是告诉我那么属性style =“?android:attr/buttonStyleSmall”是什么意思?它是否只能用于XML? – 2013-04-30 18:54:03
[样式和主题](http://developer.android.com/guide/topics/ui/themes.html#PlatformStyles)指南的最后一段似乎将属性(android:attr)描述为XML属性的可用选项,而风格(android:style)是实际的风格。我相信android:attr/buttonStyleSmall只是对风格的引用。 – 2013-04-30 18:59:20
谢谢Tanis。非常感激 – 2013-04-30 19:00:05