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

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

    I'd suggest you taking a look at this just in case before all.

    Then, if not working i'd suggest you to create your own style (like azizbekian suggest)using android xml drawables, and drawable states to differentiate pressed/notpressed.

    I think using your own style may be the best answer as it will further give you more control on how your app is displaying, but using android default themes and styles also allows the user to have custom styles which is a good idea. However, you cannot test every custom style so you cannot check that your app will display correctly on ALL custom styles, and therefore may encounter problems with some.

    0 讨论(0)
  • 2020-12-13 05:43

    In styles.xml

    <style name="MyButtonStyle" parent="Base.Widget.AppCompat.Button">
        <item name="android:background">@drawable/selector</item>
        <item name="android:textColor">@android:color/black</item>
    </style>
    

    In values/drawable:

    my_drawable.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
        <corners android:radius="2dp" />
        <!-- specify your desired color here -->
        <solid android:color="#9e9b99" />
    </shape>
    

    selector.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_focused="true" android:drawable="@drawable/my_drawable"/>
        <item android:state_pressed="true" android:drawable="@drawable/my_drawable"/>
        <item android:drawable="@android:color/transparent"/>
    </selector>
    

    In values/drawable-v21:

    my_drawable.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape android:shape="rectangle"
           android:tint="?attr/colorButtonNormal"
           xmlns:android="http://schemas.android.com/apk/res/android">
        <corners android:radius="2dp" />
        <solid android:color="@android:color/white" />
    </shape>
    

    selector.xml

    <?xml version="1.0" encoding="utf-8"?>
    <ripple xmlns:android="http://schemas.android.com/apk/res/android"
            android:color="?attr/colorControlHighlight">
        <item android:id="@android:id/mask"
              android:drawable="@drawable/my_drawable" />
    </ripple>
    

    In layout:

    <Button
        android:id="@+id/button"
        style="@style/MyButtonStyle"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:text="Test"/>
    

    Result on API 19:

    Result on API 21:

    Source code

    0 讨论(0)
  • 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:

    <Button
        android:id="@+id/sign_in_button"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Login"
        android:background="?attr/selectableItemBackground" />
                       <!--  /\ that's all -->
    

    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.

    0 讨论(0)
  • 2020-12-13 05:44

    1.add a drawable resource file named maybe button_background.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true">
            <shape>
                <solid android:color="#ff0000"/>
                <stroke android:width="5dp" android:color="#00ff00"/>
            </shape>
        </item>
        <item>
            <shape>
                <solid android:color="#00ff00"/>
            </shape>
        </item>
    </selector>
    

    2.Use the button_background.xml as the button background, done!

    github

    blog

    0 讨论(0)
  • 2020-12-13 05:46

    I think the best solution to solve that is create your own Ripple Effect. The padding when you press the button is respecting the default Ripple Effect of the component.

    <ripple xmlns:android="http://schemas.android.com/apk/res/android"
            android:color="?attr/colorControlHighlight">
        <item android:drawable="?attr/colorPrimary"/>
    </ripple>
    

    Or you can try change the style of your button to style="?android:textAppearanceSmall"

    Remember: This effect is only shown on Android Lollipop (API 21) or higher.

    0 讨论(0)
  • 2020-12-13 05:53

    As simple, use the inset property like:

    android:insetTop="0dp"
    android:insetBottom="0dp"
    android:insetRight="0dp"
    android:insetLeft="0dp"
    
    0 讨论(0)
提交回复
热议问题