This
不起作用,因为没有 buttonStyle
项目名称。它只有按钮
。
我的例子:
<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>
但这也行不通。
最佳答案
当为不同的 API 级别使用不同的主题时,我会覆盖每个主题的按钮样式。考虑以下示例。
您的 API 14+ 应用程序主题是派生自 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
没有自己的 buttonStyle
,但它的父 Theme.Holo.Light
有.按钮样式如下:
<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
,您将失去 Google 工程师为 Widget.Material.Light.Button
和 Widget.Holo 提供的定制。 Light.Button
.
我记录了几个例子来证明这一点。
Android 按钮样式。适用于所有 API 级别的 Widget.Button。
适用于所有 API 级别的老式按钮 http://youtu.be/hKWUFgvw-Gs
values 文件夹中的 styles.xml 如下所示:
<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>
每个按钮样式都源自其主题的按钮样式。
按钮有 Material 动画http://youtu.be/8Wp1TWjjha0
values-v21 文件夹中的 styles.xml 如下所示:
<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>
values-v14 文件夹中的 styles.xml 如下所示:
<resources>
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
</style>
<style name="BaseButton" parent="@android:style/Widget.Holo.Light.Button"></style>
</resources>
values 文件夹中的 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>
Material Design for Android 5.0 (API level 21)
关于android - 如何将样式应用于 Android 应用程序中的所有按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14675657/