如何应用样式到Android应用程序的所有按钮? [英] How to apply style to all buttons in Android application?
问题描述
本: http://stackoverflow.com/a/2410938/1742204
不是的作品,因为没有按钮样式的项目名称。它拥有按钮而已。
我的例子:
This: http://stackoverflow.com/a/2410938/1742204 isn't works, because there no buttonStyle item name. It has "button" only. My example:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppTheme" parent="@android:style/Theme.Black">
<item name="android:button">@style/ThemeButton</item>
</style>
<style name="ThemeButton" parent="@android:style/Widget.Button">
<item name="android:background">@drawable/button</item>
</style>
</resources>
但它不是也可以。
But it isn't works too.
推荐答案
我用不同的主题不同的API级别时,覆盖每一个主题的按钮样式。考虑下面的例子。
I override the button style for every theme when using different themes for different API levels. Consider the following example.
您的应用程序的API为14+的主题是从派生主题的android:Theme.Holo.Light.DarkActionBar
Your application's theme for API 14+ is a theme which derives from android:Theme.Holo.Light.DarkActionBar
.
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- API 14 theme customizations can go here. -->
</style>
的android:Theme.Holo.Light.DarkActionBar
没有自己的按钮样式
,但其父 Theme.Holo.Light
一样。和按钮样式如下:
android:Theme.Holo.Light.DarkActionBar
doesn't have its own buttonStyle
, but its parent Theme.Holo.Light
does. And the button style is the following:
<item name="buttonStyle">@style/Widget.Holo.Light.Button</item>
但应用程序的API为21,从提炼出来主题的android:Theme.Material.Light.DarkActionBar
<style name="AppBaseTheme" parent="android:Theme.Material.Light.DarkActionBar">
<!-- API 21 theme customizations can go here. -->
</style>
的按钮样式的android:Theme.Material.Light
如下:
<item name="buttonStyle">@style/Widget.Material.Light.Button</item>
Widget.Button
是爷爷 Widget.Holo.Light.Button
和 Widget.Material.Light.Button
我上面提到的。所以,如果你从 Widget.Button
得到您的应用程序,你就会失去 Widget.Material.Light.Button $提供的谷歌工程师的自定义C $ C>和
Widget.Holo.Light.Button
。
Widget.Button
is a grandfather of Widget.Holo.Light.Button
and Widget.Material.Light.Button
I mentioned above. So if you derive from Widget.Button
in your application, you will lose the customizations Google engineers provided for Widget.Material.Light.Button
and Widget.Holo.Light.Button
.
我录了几个例子来证明这一点。
I recorded a few examples to demonstrate this.
Android的按钮样式。 Widget.Button所有API级别。
老式按钮,所有的API级别
http://youtu.be/hKWUFgvw-Gs
The old-fashioned button for all API levels http://youtu.be/hKWUFgvw-Gs
styles.xml中的文件夹中的值的是这样的:
styles.xml in the folder values looks like this:
<style name="AppBaseTheme" parent="android:Theme.Light">
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="android:buttonStyle">@style/Button</item>
</style>
<style name="Button" parent="@android:style/Widget.Button"></style>
每一个按钮的风格从主题的按钮样式派生的。
按钮具有材质动画 http://youtu.be/8Wp1TWjjha0
styles.xml文件夹的值-V21 的中看起来是这样的:
styles.xml in the folder values-v21 looks like this:
<resources>
<style name="AppBaseTheme" parent="android:Theme.Material.Light.DarkActionBar">
<!-- API 14 theme customizations can go here. -->
</style>
<style name="BaseButton" parent="@android:style/Widget.Material.Light.Button"></style>
</resources>
styles.xml文件夹的值-V14 的中看起来是这样的:
<resources>
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
</style>
<style name="BaseButton" parent="@android:style/Widget.Holo.Light.Button"></style>
</resources>
styles.xml中的文件夹中的值的是这样的:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppBaseTheme" parent="android:Theme.Light"></style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:buttonStyle">@style/Button</item>
</style>
<style name="Button" parent="@style/BaseButton">
<item name="android:text">Lorem Ipsum</item>
</style>
</resources>
这篇关于如何应用样式到Android应用程序的所有按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!