When using Theme.MaterialComponents.Light.NoActionBar style, setting Button Background has no effect

CnPeng picture CnPeng · Oct 10, 2018 · Viewed 9.5k times · Source

In order to use Chip and ChipGroup, I set Application style extends Theme.MaterialComponents.Light.NoActionBar int manifests.xml, then I set Button "android:background" attr, but it does not effect! why? and what can I do?

This is my style:

  <style name="AppBaseTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <item name="colorPrimary">@color/primary_material_light</item>
    <item name="colorPrimaryDark">@color/header_color</item>
    <item name="actionBarSize">48dp</item>
    <item name="android:screenOrientation">portrait</item>
    <!--<item name="buttonStyle">@style/AntButton</item>-->
    <!--<item name="materialButtonStyle">@style/AntButton</item>-->
    <!--<item name="android:button"></item>-->
    <!--<item name="android:progressTint">@color/ffc000</item>-->
    <item name="colorAccent">@color/ffc000</item>
</style>


<style name="AntButton" parent="android:Widget">
    <item name="android:background">@drawable/abc_btn_default_mtrl_shape</item>
    <item name="android:textAppearance">?android:attr/textAppearanceButton</item>
    <item name="android:minHeight">48dip</item>
    <item name="android:minWidth">88dip</item>
    <item name="android:focusable">true</item>
    <item name="android:clickable">true</item>
    <item name="android:gravity">center_vertical|center_horizontal</item>
    <!--<item name="android:colorButtonNormal">@color/white</item>-->
</style>

I have tried to change buttonStyle and materialButtonStyle, but not effect too!

this is my layout XML:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/ll_popup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="@color/white"
        android:divider="@drawable/shape_divider_05dp"
        android:orientation="vertical"
        android:showDividers="beginning|middle">

        <!--only can use backgroundTint to change background color-->
        <Button
            android:id="@+id/item_popupwindows_Photo"
            android:layout_width="match_parent"
            android:layout_height="55dp"
            android:backgroundTint="@color/white"
            android:text="Pictrue"
            android:textColor="@color/c_666666"
            android:textSize="16sp" />

        <!--background not effect !!-->
        <Button
            android:id="@+id/item_popupwindows_cancel"
            android:layout_width="match_parent"
            android:layout_height="55dp"
            android:background="@color/white"
            android:text="cancel"
            android:textColor="@color/c_666666"
            android:textSize="16sp" />
    </LinearLayout>
</LinearLayout>

this is result :

enter image description here

Because I have used Chip and ChipGroup in APP, I have to user theme extends MaterialComponents! do you know how to resolve it ?please tell me, thanks!

in this page Can't use android:background with button from the new material components, the author wants to change the default padding, but I want to change the backgroundDrawable, so, it does not work for me.

Answer

Ben P. picture Ben P. · Oct 11, 2018

If you want a true Button, but one that you can modify like the framework Button (instead of the MaterialButton), then you can explicitly specify the framework version in your layout file. Replace this:

<Button
    android:id="@+id/item_popupwindows_cancel"
    ... />

with this:

<android.widget.Button
    android:id="@+id/item_popupwindows_cancel"
    ... />

This will give you what it says on the tin: an android.widget.Button, which should respond to styling the way you expect.

Similarly, you could use a <androidx.appcompat.widget.AppCompatButton> if you want support library features but not material components features.