【问题标题】:如何删除Android中按钮周围的填充?
【发布时间】:2013-07-31 09:37:43
【问题描述】:

在我的 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>

在预览和手机上,它看起来像:

正如您在底部区域的按钮上看到的那样,那里有一些填充。

我怎样才能摆脱它,让按钮完全填满底部区域?

【问题讨论】:

  • DATerre 的回答已经正确了两年。可以这样标记吗?
  • 这个问题需要编辑以获得更好的标题。它已成为 Google 搜索中的第一个结果,但并没有真正回答问题。

标签: java android button


【解决方案1】:

以编程方式删除AppCompatButton 填充:

button.setPadding(0, 0, 0, 0)
button.minHeight = 0
button.minimumHeight = 0

【讨论】:

    【解决方案2】:

    对于&lt;androidx.appcompat.widget.AppCompatButton&gt;

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

    【讨论】:

      【解决方案3】:

      对于MaterialButton,添加以下属性,它将完美地工作

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

      【讨论】:

        【解决方案4】:

        移除顶部和底部填充

        <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"/>
        

        移除左右内边距

        <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"/>
        

        【讨论】:

          【解决方案5】:

          您可以在 AppCompatButton 中使用 Borderless 样式,如下所示。并与它一起使用 android:background

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

          按钮代码

          <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" />
          

          输出:

          【讨论】:

            【解决方案6】:

            经过数小时挖掘多个答案后,我终于找到了一个解决方案,它不使用不同的元素、操纵minHeightminWidth,甚至将Button 包裹在RelativeLayout 周围。

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

            这里的主要内容是前景的设置,而不是许多答案规定的背景。将背景本身更改为selectableItemBackground 只会覆盖您对按钮颜色/背景所做的任何更改(如果有)。

            更改前景可为您带来两全其美:摆脱“不可见”的填充并保留按钮的设计。

            【讨论】:

              【解决方案7】:

              我的解决方案将 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"/>
              

              【讨论】:

              • 这也解决了我的问题!谢谢!
              • 谢谢!与solution above 配合使用效果最佳
              • 这是正确的解决方案..应该标记为正确
              【解决方案8】:

              不应该在全宽使用标准按钮,这就是您遇到这种情况的原因。

              背景

              如果您查看Material Design - Button Style,您会看到一个按钮具有 48dp 高度的点击区域,但由于...某些原因将显示为 36dp 的高度。

              这是您看到的背景轮廓,不会覆盖按钮本身的整个区域。
              它有圆角和一些内边距,应该可以自己点击,包裹其内容,而不是跨越屏幕底部的整个宽度。

              解决方案

              如上所述,您想要的是不同的背景。不是标准按钮,而是具有这种漂亮涟漪效果的可选项目的背景。

              对于这个用例,您可以将?selectableItemBackground 主题属性用于您的背景(尤其是在列表中)。
              它将添加一个平台标准波纹(或

              对于您的用例,您可以只使用以下内容:

              <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 -->
              

              如果您的视图是唯一的并且跨越整个屏幕,则也无需添加布局权重

              如果您对背景的外观有不同的想法,您必须自己创建一个自定义可绘制对象,并在那里管理颜色和状态。

              此答案复制自Question: How to properly remove padding (or margin?) around buttons in Android?

              【讨论】:

                【解决方案9】:

                这不是填充,而是背景可绘制的阴影或minHeightminWidth 的问题。

                如果您仍然想要漂亮的波纹效果,您可以使用?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>
                

                并将其应用于按钮:

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

                【讨论】:

                  【解决方案10】:

                  您也可以使用layout_width(height) 参数来实现。

                  例如我已经用android:layout_height="18dp" 代码删除了顶部和底部的空间。

                  【讨论】:

                    【解决方案11】:

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

                    【讨论】:

                      【解决方案12】:

                      我遇到了同样的问题,似乎是因为按钮的背景颜色。尝试将背景颜色更改为另一种颜色,例如:

                      android:background="@color/colorActive"
                      

                      看看它是否有效。然后,您可以定义样式,如果您希望按钮使用。

                      【讨论】:

                      • 这会删除按钮的突出显示/动画功能。
                      【解决方案13】:

                      为了移除切换按钮周围的填充,我们需要设置 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"
                              />
                      

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

                      下面是我的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>
                      

                      【讨论】:

                        【解决方案14】:

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

                        【讨论】:

                          【解决方案15】:

                          它似乎不是填充、边距或最小高度/宽度。 设置android:background="@null" 按钮会丢失其触摸动画,但事实证明,将背景设置为任何东西都可以修复该边框。


                          我目前正在与:

                          minSdkVersion 19
                          targetSdkVersion 23
                          

                          【讨论】:

                            【解决方案16】:

                            我是 android 新手,但我也遇到过类似的情况。我按照@Delyan 的建议做了,还在 xml 布局文件中使用了 android:background="@null"。

                            【讨论】:

                              【解决方案17】:

                              对我来说,问题出在某些 Android 主题的 minHeight 和 minWidth 上。

                              在 Button 元素上,添加:

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

                              或者你的按钮样式:

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

                              【讨论】:

                              • 花了一段时间才意识到我处理的不是一些奇怪的填充行为,而是 minHeight。
                              • 我不认为这个答案实际上回答了原来的问题。但是我仍然很高兴我遇到了它,因为我无法弄清楚为什么我的Button 即使在设置了android:background="@null" 之后仍然占用了这么多空间。 Button 的默认样式负责这一事实是一个非常好的提示。
                              • 同时使用 android:minHeight="0dp" android:minWidth="0dp" 很重要,而不仅仅是其中一个。
                              • 这会消除 Android Nougat 上按钮的圆角和波纹效果,知道是什么原因导致了这种行为吗?
                              • 这个答案解决了一个 GridLayout 的问题,它包含带有 autoSize 选项的按钮,因为我试图让更多按钮适合行/列。我花了很长时间才找到的简单答案...
                              【解决方案18】:

                              一种解决方法可能是尝试使用-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" />
                              

                              它会使那个空间消失。我的意思是你可以选择合适的dip 值,这样它就消失了。它对我有用。希望它对你有用。

                              【讨论】:

                              • 我会小心使用这个解决方案。它所做的只是使用负边距隐藏按钮背景(边框+阴影)。什么是样式更改和边框+阴影需要超过 5dp?为了安全起见,我宁愿使用 Delyan/Casey Murray 解决方案。
                              • 另外,负边距在 Android 中不受官方支持,所以我也会避免这种情况
                              【解决方案19】:

                              这不是填充,它是背景可绘制中按钮周围的阴影。创建你自己的背景,它就会消失。

                              【讨论】:

                              • 我做到了。没有消失。
                              • 如何测量它的大小?
                              • 就是这样,@Behr,将背景更改为按钮的颜色
                              • 我刚刚意识到我的背景图片有透明填充
                              猜你喜欢
                              • 2017-08-28
                              • 2013-01-07
                              • 2012-04-09
                              • 1970-01-01
                              • 2016-11-02
                              • 2016-12-10
                              • 2017-03-08
                              • 2013-04-29
                              • 2017-01-24
                              相关资源
                              最近更新 更多