Android 5.0 默认 Button 样式修改

本文介绍如何在Android中自定义Button的样式,特别是如何修改Button的颜色而不影响其Material Design效果。通过调整buttonStyle属性并利用预设样式,可以轻松地改变Button的外观。

文章转载自 https://www.jianshu.com/p/66c6a97a8d80

场景

在 Android 5.0 以上的设备中,API 提供的 Button 样式自带了 Material Design 风格,默认的颜色还是灰色。但在大多数情况下我们需要修改 Button 的颜色来适应我们应用的整体风格。

当我们和往常一样通过设置 android:background 属性来改变 Button 的颜色后,实际应用发现 Button 失去了 Material Design 效果,变得很一般。当尝试用 selector 来实现时,发现处理过程工作量不少,而且效果也不明显。

那么如何修改默认 Button 的颜色,而不改变默认 Button 的其它特性呢?

解决思路

1、追踪默认 Button 样式是如何设置的?

.../res/styles.xml 文件中我们可以看到应用默认的style

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

继续跟踪 Theme.AppCompat.Light.DarkActionBar

<style name="Theme.AppCompat.Light.DarkActionBar" parent="Base.Theme.AppCompat.Light.DarkActionBar"/>

继续跟踪 Base.Theme.AppCompat.Light.DarkActionBar

<style name="Base.Theme.AppCompat.Light.DarkActionBar" parent="Base.Theme.AppCompat.Light">
    <item name="actionBarPopupTheme">@style/ThemeOverlay.AppCompat.Light</item>
    <item name="actionBarWidgetTheme">@null</item>
    <item name="actionBarTheme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>

    <!-- Panel attributes -->
    <item name="listChoiceBackgroundIndicator">@drawable/abc_list_selector_holo_dark</item>

    <item name="colorPrimaryDark">@color/primary_dark_material_dark</item>
    <item name="colorPrimary">@color/primary_material_dark</item>
</style>

继续跟踪 Base.Theme.AppCompat.Light,会提示有不同版本,通过一一跟踪,最终继承的 parent style 都是 Base.V7.Theme.AppCompat.Light

<style name="Base.V7.Theme.AppCompat.Light" parent="Platform.AppCompat.Light">
    ...

    <!-- Button styles -->
    <item name="buttonStyle">@style/Widget.AppCompat.Button</item>
    <item name="buttonStyleSmall">@style/Widget.AppCompat.Button.Small</item>
    <item name="android:textAppearanceButton">@style/TextAppearance.AppCompat.Widget.Button</item>

    ...
</style>

发现其中设置了 buttonStyle,继续跟踪 Widget.AppCompat.Button

<style name="Widget.AppCompat.Button" parent="Base.Widget.AppCompat.Button"/>

继续跟踪 Base.Widget.AppCompat.Button,不同版本有不同的实现

  • v21之前
<style name="Base.Widget.AppCompat.Button" parent="android:Widget">
    <item name="android:background">@drawable/abc_btn_default_mtrl_shape</item>
    <item name="android:textAppearance">?android:attr/textAppearanceButton</item>
    <item name="android:minHeight">48dip</item>
    <item name="android:minWidth">88dip</item>
    <item name="android:focusable">true</item>
    <item name="android:clickable">true</item>
    <item name="android:gravity">center_vertical|center_horizontal</item>
</style>
  • v21及之后
<style name="Base.Widget.AppCompat.Button" parent="android:Widget.Material.Button"/>
...
<style name="Widget.Material.Button">
    <item name="background">@drawable/btn_default_material</item>
    <item name="textAppearance">?attr/textAppearanceButton</item>
    <item name="minHeight">48dip</item>
    <item name="minWidth">88dip</item>
    <item name="stateListAnimator">@anim/button_state_list_anim_material</item>
    <item name="focusable">true</item>
    <item name="clickable">true</item>
    <item name="gravity">center_vertical|center_horizontal</item>
</style>

可以看到针对不同版本分别设置了 Button 的样式。

2、通过 buttonStyle 对 Button 颜色进行设置

通过上面的分析过程得知,我们可以通过 buttonStyle 属性来指定 Button 的样式。

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <!-- 设置button的Style -->
    <item name="buttonStyle">@style/Xxx</item>
</style>

而在V7支持包中,提供了5个预置的 Button 的 Style,如下:

<style name="Widget.AppCompat.Button" parent="Base.Widget.AppCompat.Button"/>
<style name="Widget.AppCompat.Button.Colored" parent="Base.Widget.AppCompat.Button.Colored"/>
<style name="Widget.AppCompat.Button.Borderless" parent="Base.Widget.AppCompat.Button.Borderless"/>
<style name="Widget.AppCompat.Button.Borderless.Colored" parent="Base.Widget.AppCompat.Button.Borderless.Colored"/>
<style name="Widget.AppCompat.Button.Small" parent="Base.Widget.AppCompat.Button.Small"/>
style背景颜色属性文字颜色属性效果
.Button(默认)colorButtonNormaltextColor(默认黑色)这里写图片描述
.Button.ColoredcolorAccent…(默认白色)这里写图片描述
.Button.Borderless透明…(默认黑色)这里写图片描述
.Button.Borderless.Colored透明…(默认colorAccent)这里写图片描述
.Button.SmallcolorButtonNormal…(默认黑色)这里写图片描述

3、通过 ButtonStyle 修改 Button 颜色

3.1、将 ButtonStyle 应用到全局 Button

直接在应用主题中设置 buttonStyle,然后通过相应的属性修改 Button 的背景色或文字颜色。如下:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="buttonStyle">@style/Widget.AppCompat.Button</item>
    <item name="colorButtonNormal">@color/colorBlue</item>
</style>

3.2、单个具体 Button 设置

在 styles.xml 文件中新建 Style,继承应用 Style,在其中设置 buttonStyle,并通过相应的属性修改 Button 的背景色或文字颜色。然后在需要修改的 Button 中添加 android:theme 属性,引用新建的 Style 即可

<style name="RedButton" parent="AppTheme">
    <item name="buttonStyle">@style/Widget.AppCompat.Button</item>
    <item name="colorButtonNormal">@color/colorRed</item>
</style>

在 layout 中使用

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button_1"
    android:theme="@style/RedButton" />
    <!-- 注意是android:theme属性,不是style属性 -->

4、修改 Button 圆角大小

目前只找到了全局修改 Button 圆角大小的方法在 dimes.xml 文件中复写 abc_control_corner_material的值,可以修改应用内所有 Button 的圆角大小

<dimen name="abc_control_corner_material" tools:override="true">8dp</dimen>

文章只是作为个人记录学习使用,如有不妥之处请指正,谢谢。

文章转载自 https://www.jianshu.com/p/66c6a97a8d80

<think>好的,用户想修改Android按钮的文本默认全大写设置。首先,我需要回忆一下相关的知识。记得在Android中,Button控件默认会将文本转换为大写,尤其是在5.0之后的版本,这可能是因为Material Design的设计规范。用户提到的textAllCaps属性应该就是关键所在。 根据提供的引用内容,特别是引用[2],Android 5.0之后Button的textAllCaps默认是true,所以需要将其设为false。用户可能需要知道如何在XML布局中直接修改这个属性,或者全局设置。引用[3]和[4]展示了在单个Button中设置android:textAllCaps="false"的方法,而引用[1]提到最好在主题中全局设置,这样所有按钮都会应用这个修改,避免重复工作。 接下来,我需要整理步骤。首先是单个按钮的修改,直接在布局文件添加属性。然后是全局设置,通过主题样式覆盖默认值。可能还需要提到如何在代码中动态修改,但用户的问题更侧重于默认设置,所以XML和主题的方法更相关。 另外,用户可能想知道为什么会有这样的默认行为,引用[2]说明了这是Android 5.0后的变化,所以需要确认他们的目标API级别。此外,要提醒用户,如果使用AppCompatButton或其他自定义控件,可能需要检查是否有样式覆盖。 最后,生成相关问题时要考虑用户可能遇到的后续问题,比如其他控件是否有类似行为,或者如何在不同API版本中保持一致性。需要确保回答清晰,步骤正确,并且引用提供的资料来支持每个方法。</think>### 如何在Android中更改按钮文本默认全大写的属性? #### 方法1:在XML布局中直接修改单个按钮属性 在按钮的XML定义中添加`android:textAllCaps="false"`即可禁用该按钮的默认全大写显示。例如: ```xml <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAllCaps="false" // 强制关闭大写转换 android:text="@string/button_text" /> ``` 此方法适用于仅需修改特定按钮的场景[^3][^4]。 #### 方法2:全局修改主题样式(推荐) 在`styles.xml`中自定义主题,覆盖默认的按钮样式: ```xml <!-- 在res/values/styles.xml中 --> <style name="AppTheme" parent="Theme.AppCompat.Light"> <item name="buttonStyle">@style/MyButtonStyle</item> </style> <style name="MyButtonStyle" parent="Widget.AppCompat.Button"> <item name="android:textAllCaps">false</item> // 全局禁用大写转换 </style> ``` 通过主题设置,所有使用默认样式的按钮都会应用此修改[^1][^2]。 #### 补充说明 - **兼容性**:此问题主要出现在Android 5.0(API 21)及以上版本,因其默认启用了Material Design规范的全大写按钮样式。 - **代码动态修改**:可通过`button.setTransformationMethod(null)`在Java/Kotlin代码中实现。
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值