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

为君一笑 提交于 2020-05-24 21:19:33

问题


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 :

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.


回答1:


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.




回答2:


In this particular case, the LinearLayout holding your second Button seems to have a white background color. That means you don't need to explicitly specify a white background for your Button; you can just let the LinearLayout's background show through.

This can be accomplished by using the "Text Button" style of MaterialButton:

style="@style/Widget.MaterialComponents.Button.TextButton"


来源:https://stackoverflow.com/questions/52743190/when-using-theme-materialcomponents-light-noactionbar-style-setting-button-back

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!