Как сделать заглавную кнопку с текстом 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>

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>

Второй метод изменит его для всех кнопок в приложении. Другим способом является расширение кнопки и создание пользовательской кнопки, в которой для текстовых колпачков задано значение true и используется эта кнопка вместо кнопки по умолчанию.

Другие вопросы по тегам