[英]How to properly remove padding (or margin?) around buttons in Android?
[英]How to remove padding around buttons in Android?
在我的 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>
在预览版和手机上,它看起来像:
正如您在底部区域的按钮上看到的那样,那里有一些填充。
我怎样才能摆脱它,让按钮完全填满底部区域?
对我来说,问题出在某些 Android 主题上的 minHeight 和 minWidth 上。
在 Button 元素上,添加:
<Button android:minHeight="0dp" android:minWidth="0dp" ...
或者以您的按钮样式:
<item name="android:minHeight">0dp</item>
<item name="android:minWidth">0dp</item>
我的解决方案将 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"/>
这不是填充,它是背景可绘制中按钮周围的阴影。 创建你自己的背景,它会消失。
对于 MaterialButton,添加以下属性,它将完美运行
<android.support.design.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:insetTop="0dp"
android:insetBottom="0dp"/>
一种解决方法可能是尝试使用-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
值,让它消失。 它对我有用。 希望对你有效。
删除顶部和底部填充
<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"/>
为了移除切换按钮周围的填充,我们需要将 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>
我是android新手,但我也有类似的情况。 我按照@Delyan 的建议做了,还在 xml 布局文件中使用了 android:background="@null" 。
您应该在按钮中使用minHeight
和minWidth
属性,并确保使用padding
android:minHeight="0dp"
android:minWidth="0dp"
我有同样的问题,似乎是因为按钮的背景颜色。 尝试将背景颜色更改为另一种颜色,例如:
android:background="@color/colorActive"
看看它是否有效。 然后,如果您希望按钮使用,您可以定义样式。
您可以通过将其插图设置为零来实现此目的。
android:insetBottom="0dp"
android:insetTop="0dp"
在按钮的 XML 集android:includeFontPadding="false"
这不是填充,而是背景可绘制的阴影或minHeight
和minWidth
的问题。
如果您仍然想要漂亮的涟漪效果,您可以使用?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"
... />
您可以在AppCompatButton中使用无边框样式,如下所示。 并使用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" />
输出:
它似乎不是填充、边距或最小高度/宽度。 设置android:background="@null"
按钮会丢失其触摸动画,但事实证明,将背景设置为任何东西都可以修复该边框。
我目前正在与:
minSdkVersion 19
targetSdkVersion 23
标准按钮不应该以全宽使用,这就是您遇到这种情况的原因。
如果您查看Material Design - Button Style ,您会看到一个按钮有一个 48dp 高度的点击区域,但出于某种原因,它会显示为 36dp 的高度。
这是您看到的背景轮廓,它不会覆盖按钮本身的整个区域。
它有圆角和一些内边距,应该可以自己点击,包裹其内容,而不是跨越屏幕底部的整个宽度。
如上所述,您想要的是不同的背景。 不是标准按钮,而是具有这种漂亮涟漪效果的可选项目的背景。
对于这个用例,您可以将?selectableItemBackground
主题属性用于您的背景(尤其是在列表中)。
它将添加一个平台标准波纹(或 < 21 上的一些颜色状态列表),并将使用您当前的主题颜色。
对于您的用例,您可能只使用以下内容:
<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 -->
如果您的视图是唯一的并且跨越整个屏幕,则也无需添加布局权重
如果你对你的背景应该是什么样子有不同的想法,你必须自己创建一个自定义的可绘制对象,并在那里管理颜色和状态。
对于<androidx.appcompat.widget.AppCompatButton>
<androidx.appcompat.widget.AppCompatButton
.
.
android:minHeight="0dp"
android:minWidth="0dp"
>
</androidx.appcompat.widget.AppCompatButton>
给你的按钮一个自定义背景:@drawable/material_btn_blue
经过数小时挖掘多个答案后,我终于找到了一个不使用不同元素、不操作minHeight
或minWidth
甚至将Button
包裹在RelativeLayout
周围的解决方案。
<Button
android:foreground="?attr/selectableItemBackground"
android:background="@color/whatever" />
这里的主要内容是前景的设置,而不是许多答案规定的背景。 将背景本身更改为selectableItemBackground
只会覆盖您对按钮的颜色/背景所做的任何更改(如果有)。
更改前景可为您提供两全其美的效果:摆脱“隐形”填充并保留按钮的设计。
以编程方式删除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"
为我工作:)
android:insetTop="0dp"
android:insetBottom="0dp"
android:minWidth="0dp"
android:minHeight="0dp"
android:padding="0dp"
您也可以使用layout_width(height)
参数来实现。
例如,我使用android:layout_height="18dp"
代码删除了顶部和底部空间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.