android - 如何将样式应用于 Android 应用程序中的所有按钮?

标签 android button styles

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

Material Design for Android 5.0 (API level 21)

How to record your screen on Android 4.4.4

Video bitrates

关于android - 如何将样式应用于 Android 应用程序中的所有按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14675657/

相关文章:

swift - 让按钮图像贴在左侧 swift xcode

wpf - Path.Data 样式仅适用于样式对象的第一个实例

javascript - 如何访问 :after and :before properties of box-shadow in javascript

android - 如何为每个 Activity 的操作栏 Sherlock 创建自定义样式

php - 规则=行在 PHP 中不起作用

java - NDK 与 JNI 同时调用返回类型为非原始类型的方法

屏幕方向改变后Android进度条不更新

android - AutoCompleteTextView:从先前提出的建议列表中过滤结果

android - 使用 facebook android sdk 应用程序崩溃 API 4 登录

android - 单击时不要将按钮置于前台