如何在Lollipop之前大写按钮文字?

时间:2016-06-20 16:08:17

标签: android textview android-button android-styles android-textattributes

我尝试将其添加到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>

1 个答案:

答案 0 :(得分:1)

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

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

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