简体   繁体   中英

Change default button style

I want to define a default style for my buttons so that I don't have to type style="@style/mainButtonStyle" inside of each button manually. I have already defined the style.

    <style name="mainButtonStyle" parent="android:Widget.Material.Button">
        <item name="android:background">@drawable/button_default_background</item>
        <item name="android:textAllCaps">true</item>
        <item name="android:textStyle">bold</item>
        <item name="android:fontFamily">sans-serif-light</item>
    </style>

And already added an android:buttonStyle attribute in the app's theme.

themes.xml

 <!-- Base application theme. -->
    <style name="Theme.PlanShopping" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">#AA0000</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <item name="colorSurface">@color/black</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
        <item name="android:buttonStyle">@style/mainButtonStyle</item>
    </style>

The Android Manifest has android:theme attribute set up by default.

    <application
        android:name=".PlannerApplication"
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.PlanShopping"
        tools:targetApi="31">
        ...
    </application>

So why is it not changing the default button style? The style works fine if I type it manually inside of a button. But it's not the result I wanted. How can I set it by default? I've tried to inherit from different types of buttons: android:Widget.Button , android:Widget.Holo.Button . Also, different <Button> types did not help. For now, I'm using a default <Button> component (not material or holo).

If the button is a MaterialButton you will need to specify:

<item name="materialButtonStyle">@style/mainButtonStyle</item>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM