How to properly style material buttons in android

2019-04-14 11:45发布

问题:

I am going after flat buttons for my example, but the question really applies to all buttons. Why does it seem so hard to find documentation on the proper way to style material buttons in android.

I have the following which I messed around with until I could get it to work. Felt like a hack so I am not sure if this is really the right way to do it.

    <Button
        android:id="@+id/apply"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/md_cyan_A700"
        android:text="Cancel"/>

So I used 'borderlessButtonStyle' to get a flat button. And I had to change the textColor to the material color I wanted for the button.

Biggest thing is I feel like the animations are wrong when I do it like this. I don't get a nice ripple across the button, instead I get a grey circle based on where I click. And the circle gets cut off if I click near edges. Just feels wrong.

Again the color looks right but I am not sure this is the 'correct' way to do this. Why is there not 'flat' style for a material button out of the box. It is a regular material style.

回答1:

Add:

android:background="?android:attr/selectableItemBackgroundBorderless"