How to properly remove padding (or margin?) around buttons in Android?

前端 未结 13 984
醉梦人生
醉梦人生 2020-12-13 04:50

Currently, I have the following bottom log in button.

When button is not being pressed

When button is being pressed

The XML loo

13条回答
  •  一生所求
    2020-12-13 05:44

    A standard button is not supposed to be used at full width which is why you experience this.

    Background

    If you have a look at the Material Design - Button Style you will see that a button has a 48dp height click area, but will be displayed as 36dp of height for...some reason.

    This is the background outline you see, which will not cover the whole area of the button itself.
    It has rounded corners and some padding and is supposed to be clickable by itself, wrap its content, and not span the whole width at the bottom of your screen.

    Solution

    As mentioned above, what you want is a different background. Not a standard button, but a background for a selectable item with this nice ripple effect.

    For this use case there is the ?selectableItemBackground theme attribute which you can use for your backgrounds (especially in lists).
    It will add a platform standard ripple (or some color state list on < 21) and will use your current theme colors.

    For your usecase you might just use the following:

    There is also no need to add layout weights if your view is the only one and spans the whole screen

    If you have some different idea on what your background should look like you have to create a custom drawable yourself, and manage color and state there.

提交回复
热议问题