【问题标题】:How to capitalize button text pre-Lollipop?如何在棒棒糖前大写按钮文本?
【发布时间】:2016-06-20 16:08:16
【问题描述】:

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

【问题讨论】:

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


    【解决方案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>
    

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

    【讨论】:

    • 我假设 false 应该是 true
    • 成功了!我之前是这样做的,但不是 @style/MyButton 我在做 @style/MyButton。什么时候加前缀“android:”什么时候不加前缀?
    • 对于 android 特定名称,您将使用 android:。对于自定义主题名称,无需使用 android:。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-03
    • 2015-08-12
    • 1970-01-01
    相关资源
    最近更新 更多