如何将棒棒糖前的按钮文本大写?

How to capitalize button text pre-Lollipop?

我尝试像这样将它添加到 styles.xml 中:

<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Main theme colors -->
        <!--   your app branding color for the app bar -->
        <item name="colorPrimary">@color/primary</item>
        <!--   darker variant for the status bar and contextual app bars -->
        <item name="colorPrimaryDark">@color/primary_dark</item>
        <!--   theme UI controls like checkboxes and text fields -->
        <item name="colorAccent">@color/accent</item>

        <!-- buttons will not have shadows -->
        <item name="android:buttonStyle">@style/Widget.AppCompat.Button.Borderless</item>
        <item name="android:textAppearanceButton">@style/DefaultButtonTextStyle</item>

        <item name="android:colorBackground">@android:color/white</item>

    </style>

<!--The button text style to be used throughout the app-->
    <style name="DefaultButtonTextStyle" parent="@style/Base.TextAppearance.AppCompat.Button">
        <!-- Ensure that the buttons are all caps even pre Lollipop-->
        <item name="android:textAllCaps">true</item>
        <item name="textAllCaps">true</item>
    </style>

您只需为您的按钮创建自定义样式

<style name="CustomButton">
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_centerVertical">true</item>
    <item name="android:textAllCaps">true</item>
</style>

然后将其设置为您的按钮,例如:

<Button 
     android:id="@+id/total_bill"
     style="@style/CustomButton" />

或者您可以这样做:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="buttonStyle">@style/MyButton</item>
    </style>

    <style name="MyButton" parent="Widget.AppCompat.Button">
        <item name="android:textAllCaps">true</item>
    </style>

第二种方法将为整个 application.One 中的所有 Button 更改它,另一种方法是扩展 Button 并创建一个自定义 Button,其中 textcaps 设置为 true 并使用该 Button 而不是默认 Button。