【问题标题】:Android - default button styleAndroid - 默认按钮样式
【发布时间】:2012-10-24 17:49:44
【问题描述】:

问题: 在哪里可以找到具有十六进制颜色代码的默认样式 xml?

我正在寻找 Style 'buttonStyle' 和其他影响 TextView、Button 等方面的默认样式(如果您不更改方面的样式)

我在<instalation_folder>\android-sdk\platforms\android-<versio>\data\res\values<instalation_folder>\android-sdk\platforms\android-<version>\data\res\colors 中查找,但实际上并没有找到我要找的东西。

希望我的问题很清楚。


由于声誉低,我还不能回答这个问题。这里是答案

回答

通过谷歌搜索,我发现 'buttonStyle' 实际上是 'Widget.Button' - Styling Android With Defaults

这就是它的工作原理:

  • 正如我所说,'buttonStyle' 样式是在\android-sdk\platforms\android-<version>\data\res\values\styles.xml 中定义的精确'Widget.Button' 样式。背景设置为:@android:drawable/btn_default
  • \android-sdk\platforms\android-<version>\data\res\drawable\btn_default.xml 将按钮的背景颜色定义为选择器。颜色实际上取决于按钮的状态。默认颜色设置为@drawable/btn_default_normal
  • 经过一番搜索,我发现 btn_default_normal 是位于 \android-sdk\platforms\android-<version>\data\res\drawable-mdpi 的 png 图像

我觉得这有点令人困惑,但我希望它对某人有所帮助,也许......

【问题讨论】:

    标签: java android button styles


    【解决方案1】:

    了解 Android 样式的工作原理可能有点混乱。

    我将尝试通过一个示例来解释基本的工作流程。

    假设您想知道按钮的默认背景是什么。 这可以是简单的颜色(不太可能)或可绘制对象(有许多不同类型的可绘制对象)。

    Android 有主题。主题基本上定义了将哪种样式应用于哪个小部件。 因此,我们的第一步是找到默认的android主题。

    您可以在android-sdk\platforms\android-15\data\res\values\themes.xml 下找到它

    在这个主题文件中,搜索button

    你会发现这样的东西:

    <!-- Button styles -->
    
    <item name="buttonStyle">@android:style/Widget.Button</item>
    

    这意味着主题将样式Widget.Button 应用于按钮。

    好的,现在让我们找到样式Widget.Button

    所有默认的Android风格都定义在文件android-sdk\platforms\android-15\data\res\values\styles.xml

    现在搜索Widget.Button

    你会发现这样的东西:

    <style name="Widget.Button">
        <item name="android:background">@android:drawable/btn_default</item>
        <item name="android:focusable">true</item>
        <item name="android:clickable">true</item>
        <item name="android:textAppearance">?android:attr/textAppearanceSmallInverse</item>
        <item name="android:textColor">@android:color/primary_text_light</item>
        <item name="android:gravity">center_vertical|center_horizontal</item>
    </style>
    

    有趣的是:

    &lt;item name="android:background"&gt;@android:drawable/btn_default&lt;/item&gt;

    这意味着有一个名为btn_default 的drawable 设置为按钮背景。

    现在我们需要在android-sdk\platforms\android-15\data\res 下的一个可绘制文件夹中找到一个名为btn_default.* 的文件。

    这可以是图像(不太可能)或像 btn_default.xml 这样的 xml 文件。

    稍微搜索一下你会找到文件android-sdk\platforms\android-15\data\res\drawable\btn_default.xml

    它包含如下内容:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_window_focused="false" android:state_enabled="true" android:drawable="@drawable/btn_default_normal" />
        <item android:state_window_focused="false" android:state_enabled="false" android:drawable="@drawable/btn_default_normal_disable" />
        <item android:state_pressed="true" android:drawable="@drawable/btn_default_pressed" />
        <item android:state_focused="true" android:state_enabled="true" android:drawable="@drawable/btn_default_selected" />
        <item android:state_enabled="true" android:drawable="@drawable/btn_default_normal" />
        <item android:state_focused="true" android:drawable="@drawable/btn_default_normal_disable_focused" />
        <item android:drawable="@drawable/btn_default_normal_disable" />
    </selector>
    

    现在你必须明白这是一个可绘制的选择器(众多可绘制类型之一)。 此选择器根据按钮状态选择不同的背景。例如,如果按下按钮,则它具有不同的背景。

    不让我们看一下默认状态。

    &lt;item android:state_enabled="true" android:drawable="@drawable/btn_default_normal" /&gt;

    它应用了一个名为btn_default_normal的drawable。

    现在我们需要找到这个可绘制对象。

    同样,我们需要在android-sdk\platforms\android-15\data\res 下的一个可绘制文件夹中找到一个名为btn_default_normal.* 的文件。

    这又可以是图像或 xml 文件,例如 btn_default_normal.xml

    您会在不同的可绘制文件夹中找到多个名为“btn_default_normal.9.png”的文件,用于不同的分辨率。

    :) 现在您知道btn_default_normal.9.png 被设置为按钮背景。

    【讨论】:

    • 谢谢!!解释得很好。
    • 哇,这是一个很好的答案:)
    • 很好解释,但是你如何搜索Widget.Button 并知道它在 android-sdk\platforms\android-15\data\res\values\styles.xml 下的这个文件路径中
    • 当前的 Android Studio 默认项目在其build.gradle 中有implementation 'com.google.android.material:material:1.4.0',这会以某种方式改变小部件的外观。
    【解决方案2】:

    在 XML 中声明时,您可以找到 android 小部件的默认样式:

    style="@android:style/Widget.Button" - standard Button
    style="@android:style/Widget.TextView" - standard TextView
    

    例如这是默认按钮的样式:

        <style name="Widget.Button">
            <item name="android:background">@android:drawable/btn_default</item>
            <item name="android:focusable">true</item>
            <item name="android:clickable">true</item>
            <item name="android:textAppearance">?android:attr/textAppearanceSmallInverse</item>
            <item name="android:textColor">@android:color/primary_text_light</item>
            <item name="android:gravity">center_vertical|center_horizontal</item>
        </style>
    

    默认按钮背景:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/btn_default_normal" android:state_enabled="true" android:state_window_focused="false"/>
        <item android:drawable="@drawable/btn_default_normal_disable" android:state_enabled="false" android:state_window_focused="false"/>
        <item android:drawable="@drawable/btn_default_pressed" android:state_pressed="true"/>
        <item android:drawable="@drawable/btn_default_selected" android:state_enabled="true" android:state_focused="true"/>
        <item android:drawable="@drawable/btn_default_normal" android:state_enabled="true"/>
        <item android:drawable="@drawable/btn_default_normal_disable_focused" android:state_focused="true"/>
        <item android:drawable="@drawable/btn_default_normal_disable"/>
    </selector> 
    

    您可以在任何图形编辑器的帮助下获得默认颜色: 默认按钮的路径NinePatch ..\android-sdk\platforms\android-13\data\res\drawable-hdpi

    【讨论】:

    • 如何为 Appcompat 库执行此操作? android:Widget.Button 到@style/Widget.AppCompat.Button 也不行!!
    猜你喜欢
    • 1970-01-01
    • 2015-09-01
    • 1970-01-01
    • 2016-01-18
    • 1970-01-01
    • 2011-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多