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

前端 未结 13 1024
醉梦人生
醉梦人生 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:37

    The padding and margin may be a result of the original resources used in the button.

    So you could try to change the resources used, using a selector:

    
        
        
        
        
    
    

    That would change the default images/shapes for your buttons, so you could try using drawables and set every item to a drawable. The drawable being either a bitmap, or a .xml file(style file) defining the look of the button in its current state. I assume there still are some native styles included even though you have set the button-style yourself. This may be because you aren't using a custom theme. So the issue may also be solved by defing

    theme="@style/myNewTheme"
    

    where myNewTheme is your theme, and it should have any parents(parent="" should not be defined).

    Take any given theme(designed by Google/Android, for an instance Theme.AppCompat.[name]), it does also come with a buttonStyle. This is a part of Theme.Holo.Light:

        
        @style/Widget.Holo.Light.Button
    
        @style/Widget.Holo.Light.Button.Small
        @style/Widget.Holo.Light.Button.Inset
    
        @style/Widget.Holo.Light.Button.Toggle
        @style/Widget.Holo.Light.CompoundButton.Switch
        @style/Widget.Holo.Light.MediaRouteButton
    
        @drawable/item_background_holo_light
        ?attr/selectableItemBackground
        @style/Widget.Holo.Light.Button.Borderless
        @drawable/ic_ab_back_holo_light
    

    As you see, this theme defines how your buttons will look/work in basic features. You can override parts of it, but you haven't overridden the important parts(being buttonStyle and similar). So if you create a new theme yourself and style it to your liking and set the theme(using theme="themename") and that theme does not inherit any theme, you should be able to style your buttons to your liking without having to worry about the default styles in the theme

    Basically:

    calling padding/margin="0dp" will not help. The default drawable defined by the theme has this in the button drawable, meaning you cannot change it. So you have to either change the button style, or change the theme completely. Make sure that theme does not have any parents, because many themes define the button style. You do not want the button style defined by the theme.

提交回复
热议问题