安卓-默认按钮样式

51

问题:

我在寻找Style 'buttonStyle'和其他默认样式,这些样式会影响到像TextViews、Buttons等方面(如果您不改变方面的样式)。

我在<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 图像。

我觉得有点混乱,但希望它能帮助某个人,也许...

2个回答

145

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

有趣的一行是:

<item name="android:background">@android:drawable/btn_default</item>

这意味着有一个名为btn_default的可绘制对象被设置为按钮背景。

现在我们需要在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>

现在你需要明白,这是一个选择器drawable(许多drawable类型之一)。 根据按钮的状态,此选择器会选择不同的背景。例如,如果按钮被按下,它将具有不同的背景。

现在让我们看看默认状态。

<item android:state_enabled="true" android:drawable="@drawable/btn_default_normal" />

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

现在我们需要找到这个drawable。

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

这可以是图像,也可以是像btn_default_normal.xml一样的xml文件。

您会在不同的drawable文件夹中找到多个名为'btn_default_normal.9.png'的文件,用于不同的分辨率。

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


1
非常感谢!解释得非常清楚。 - Zombie
7
哇,这是一个很棒的回答 :) - theapache64
讲解得非常好,但是你如何搜索 Widget.Button 并知道它在 android-sdk\platforms\android-15\data\res\values\styles.xml 文件路径下呢? - Jongz Puangput
当前的Android Studio默认项目在它们的build.gradle中有implementation 'com.google.android.material:material:1.4.0',这会改变小部件的外观。 - AndreKR

9

在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


2
如何在Appcompat库中实现这个?android:Widget.Button到@style/Widget.AppCompat.Button不起作用! - LOG_TAG

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