简体   繁体   English

如何删除 Android 中按钮周围的填充?

[英]How to remove padding around buttons in Android?

In my Android app, I have this layout:在我的 Android 应用程序中,我有这样的布局:

<?xml version="1.0" encoding="utf-8" ?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical">

    <fragment
         android:id="@+id/map"
         android:layout_width="match_parent"
         android:layout_height="0dp" 
         android:layout_weight="1" 
         class="com.google.android.gms.maps.SupportMapFragment"/>

    <Button
        android:id="@+id/button_back"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="CloseActivity"
        android:padding="0dp"
        android:text="@+string/back" />

</LinearLayout>

In the preview and on the phone, it looks like:在预览版和手机上,它看起来像:

As you can see on the button in the bottom area, there is some padding there.正如您在底部区域的按钮上看到的那样,那里有一些填充。

How can I get rid of that, and let the button fully fill the bottom area?我怎样才能摆脱它,让按钮完全填满底部区域?

For me the problem turned out to be minHeight and minWidth on some of the Android themes.对我来说,问题出在某些 Android 主题上的 minHeight 和 minWidth 上。

On the Button element, add:在 Button 元素上,添加:

<Button android:minHeight="0dp" android:minWidth="0dp" ...

Or in your button's style:或者以您的按钮样式:

<item name="android:minHeight">0dp</item>
<item name="android:minWidth">0dp</item>

My solution was set to 0 the insetTop and insetBottom properties.我的解决方案将 insetTop 和 insetBottom 属性设置为 0。

<android.support.design.button.MaterialButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:insetTop="0dp"
        android:insetBottom="0dp"
        android:text="@string/view_video"
        android:textColor="@color/white"/>

That's not padding, it's the shadow around the button in its background drawable.这不是填充,它是背景可绘制中按钮周围的阴影。 Create your own background and it will disappear.创建你自己的背景,它会消失。

for MaterialButton, add below properties, it will work perfectly对于 MaterialButton,添加以下属性,它将完美运行

<android.support.design.button.MaterialButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:insetTop="0dp"
    android:insetBottom="0dp"/>

A workaround may be to try to use -ve values for margins like following:一种解决方法可能是尝试使用-ve值作为边距,如下所示:

<Button
    android:id="@+id/button_back"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:onClick="CloseActivity"
    android:padding="0dp"
    android:layout_marginLeft="-5dip"
    android:layout_marginRight="-5dip"
    android:layout_marginTop="-5dip"
    android:layout_marginBottom="-5dip"
    android:text="@string/back" />

It will make that space vanish.它会使那个空间消失。 I mean you can choose the appropriate dip value, which makes it go away.我的意思是你可以选择适当的dip值,让它消失。 It worked for me.它对我有用。 Hope it works for you.希望对你有效。

To remove the Top and Bottom padding删除顶部和底部填充

<com.google.android.material.button.MaterialButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minHeight="0dp"//to effect the following parameters, this must be added!
        android:insetTop="0dp"
        android:insetBottom="0dp"/>

To remove the Left and Right padding删除左右填充

<com.google.android.material.button.MaterialButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="0dp"//to effect the following parameters, this must be added!
        android:insetLeft="0dp"
        android:insetRight="0dp"/>

For removing padding around toggle button we need set minWidth and minHeight 0dp.为了移除切换按钮周围的填充,我们需要将 minWidth 和 minHeight 设置为 0dp。 android:minWidth="0dp" android:minHeight="0dp"

  <ToggleButton
        android:id="@+id/toggle_row_notifications"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="0dp"
        android:minHeight="0dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:padding="@dimen/_5sdp"
        android:textOn=""
        android:textOff=""
        android:background="@android:color/transparent"
        android:button="@drawable/toggle_selector"
        />

set your drawable file to button attribute like: android:button="@drawable/toggle_selector"将您的可绘制文件设置为按钮属性,例如: android:button="@drawable/toggle_selector"

Below is my toggle_selecter.xml file下面是我的toggle_selecter.xml文件

<?xml version="1.0" encoding="utf-8"?>
     <selector xmlns:android="http://schemas.android.com/apk/res/android">
            <item android:drawable="@drawable/notifications_toggle_on" 
                  android:state_checked="true"/>
            <item android:drawable="@drawable/notifications_toggle_off" 
                  android:state_checked="false"/>
     </selector>

I am new to android but I had a similar situation.我是android新手,但我也有类似的情况。 I did what @Delyan suggested and also used android:background="@null" in the xml layout file.我按照@Delyan 的建议做了,还在 xml 布局文件中使用了 android:background="@null" 。

You should use minHeight and minWidth attributes in the button and make sure of the use of padding 您应该在按钮中使用minHeightminWidth属性,并确保使用padding

    android:minHeight="0dp"
    android:minWidth="0dp"

I had the same problem and it seems that it is because of the background color of the button.我有同样的问题,似乎是因为按钮的背景颜色 Try changing the background color to another color eg:尝试将背景颜色更改为另一种颜色,例如:

android:background="@color/colorActive"

and see if it works.看看它是否有效。 You can then define a style if you want for the button to use.然后,如果您希望按钮使用,您可以定义样式

You can achieve this by setting its inset to zero.您可以通过将其插图设置为零来实现此目的。

android:insetBottom="0dp"
android:insetTop="0dp"

在按钮的 XML 集android:includeFontPadding="false"

It's not a padding but or the shadow of the background drawable or a problem with the minHeight and minWidth .这不是填充,而是背景可绘制的阴影或minHeightminWidth的问题。

If you still want the nice ripple affect, you can make your own button style using the ?attr/selectableItemBackground :如果您仍然想要漂亮的涟漪效果,您可以使用?attr/selectableItemBackground制作自己的按钮样式:

<style name="Widget.AppTheme.MyCustomButton" parent="Widget.AppCompat.Button.Borderless">
    <item name="android:minHeight">0dp</item>
    <item name="android:minWidth">0dp</item>
    <item name="android:layout_height">48dp</item>
    <item name="android:background">?attr/selectableItemBackground</item>
</style>

And apply it to the button:并将其应用于按钮:

<Button 
    style="@style/Widget.AppTheme.MyCustomButton"
    ... />

You can use Borderless style in AppCompatButton like below.您可以在AppCompatButton中使用无边框样式,如下所示。 and use android:background with it.并使用android:background

style="@style/Widget.AppCompat.Button.Borderless.Colored" style="@style/Widget.AppCompat.Button.Borderless.Colored"

Button code按钮代码

<androidx.appcompat.widget.AppCompatButton
        android:id="@+id/button_visa_next"
        android:background="@color/colorPrimary"
        style="@style/Widget.AppCompat.Button.Borderless.Colored"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/spacing_normal"
        android:text="@string/next"
        android:textColor="@color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

Output:输出:

在此处输入图像描述

It doesn't seem to be padding, margin, or minheight/width.它似乎不是填充、边距或最小高度/宽度。 Setting android:background="@null" the button loses its touch animation, but it turns out that setting the background to anything at all fixes that border.设置android:background="@null"按钮会丢失其触摸动画,但事实证明,将背景设置为任何东西都可以修复该边框。


I am currently working with:我目前正在与:

minSdkVersion 19
targetSdkVersion 23

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.如果您查看Material Design - Button Style ,您会看到一个按钮有一个 48dp 高度的点击区域,但出于某种原因,它会显示为 36dp 的高度。

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).对于这个用例,您可以将?selectableItemBackground主题属性用于您的背景(尤其是在列表中)。
It will add a platform standard ripple (or some color state list on < 21) and will use your current theme colors.它将添加一个平台标准波纹(或 < 21 上的一些颜色状态列表),并将使用您当前的主题颜色。

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.如果你对你的背景应该是什么样子有不同的想法,你必须自己创建一个自定义的可绘制对象,并在那里管理颜色和状态。

This answer copied from Question: How to properly remove padding (or margin?) around buttons in Android?这个答案复制自问题:如何正确删除 Android 中按钮周围的填充(或边距?)?

For <androidx.appcompat.widget.AppCompatButton>对于<androidx.appcompat.widget.AppCompatButton>

           <androidx.appcompat.widget.AppCompatButton
                .
                .
                android:minHeight="0dp"
                android:minWidth="0dp" 
               >

            </androidx.appcompat.widget.AppCompatButton>

给你的按钮一个自定义背景:@drawable/material_btn_blue

After hours of digging around multiple answers I finally found a solution that doesn't use a different element, manipulate minHeight or minWidth , or even wrapping Button around a RelativeLayout .经过数小时挖掘多个答案后,我终于找到了一个不使用不同元素、不操作minHeightminWidth甚至将Button包裹在RelativeLayout周围的解决方案。

<Button
    android:foreground="?attr/selectableItemBackground"
    android:background="@color/whatever" />

The main takeaway here is the setting of the foreground instead of background as many of the answers stipulate.这里的主要内容是前景的设置,而不是许多答案规定的背景。 Changing the background itself to selectableItemBackground will just overwrite any changes you made to the button's color/background, if any.将背景本身更改为selectableItemBackground只会覆盖您对按钮的颜色/背景所做的任何更改(如果有)。

Changing the foreground gives you the best of both worlds: get rid of the "invisible" padding and retain your button's design.更改前景可为您提供两全其美的效果:摆脱“隐形”填充并保留按钮的设计。

To remove AppCompatButton paddings programmatically:以编程方式删除AppCompatButton填充:

button.setPadding(0, 0, 0, 0)
button.minHeight = 0
button.minimumHeight = 0
Add these properties:

    android:insetTop="0dp"
    android:insetBottom="0dp"
    android:minWidth="0dp"
    android:minHeight="0dp"
    android:padding="0dp"

Worked for me:)为我工作:)

   android:insetTop="0dp"
    android:insetBottom="0dp"
    android:minWidth="0dp"
    android:minHeight="0dp"
    android:padding="0dp"

You can also do it with layout_width(height) parameters.您也可以使用layout_width(height)参数来实现。

Eg I have deleted top and bottom space with android:layout_height="18dp" code.例如,我使用android:layout_height="18dp"代码删除了顶部和底部空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM