如何在Android应用中为所有按钮应用样式?

3

这个无法工作,因为没有buttonStyle项目名称。它只有button

我的例子:

<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>

但是这也不起作用。

是的,有buttonStyle项目。您确定在应用程序中使用了Theme.Black吗? - Michał Z.
是的。但是没有buttonStyle,看我的屏幕:http://s43.radikal.ru/i100/1302/f1/012f48561ebd.png - Maximus
5
不用在意 Eclipse 没有显示它,它是存在的,尝试输入并运行你的应用程序即可。 - Michał Z.
好的,当我强制使用它时,它可以工作。谢谢... - Maximus
这并不是强制的。Eclipse有时候也不完美 :) - Michał Z.
1个回答

6

当使用不同的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.ButtonWidget.Material.Light.Button的祖先。因此,如果您在应用程序中派生自Widget.Button,您将失去Google工程师为Widget.Material.Light.ButtonWidget.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>

安卓5.0(API 21)的Material Design

如何在安卓4.4.4上录制屏幕

视频比特率


为什么在API 16级之后,这个功能停止工作?我无法为所有按钮设置样式。 - Michael
@Michail,请问你能否把你的代码发布到Github上呢? - Maksim Dmitriev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接