Java 如何删除Android中按钮周围的填充?
在我的Android应用程序中,我有以下布局:Java 如何删除Android中按钮周围的填充?,java,android,button,Java,Android,Button,在我的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:o
<?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>
在预览和手机上,它看起来像:
正如您在底部区域的按钮上看到的,那里有一些填充
我怎样才能摆脱它,让按钮完全填满底部区域?这不是填充,而是按钮周围的阴影在其背景中可绘制。创建您自己的背景,它将消失。一种解决方法可能是尝试使用
-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
值,这样它就会消失。这对我有用。希望它对你有用。我不熟悉android,但我也有过类似的情况。我按照@Delyan的建议做了,并在xml布局文件中使用了android:background=“@null”。对我来说,一些android主题的问题是minHeight和minWidth
在按钮元素上,添加:
<Button android:minHeight="0dp" android:minWidth="0dp" ...
它似乎不是填充、边距或最小高度/宽度。
设置android:background=“@null”
按钮会丢失其触摸动画,但事实证明,将背景设置为任何东西都可以修复该边框
我目前的工作是:
minSdkVersion 19
targetSdkVersion 23
在按钮的XML集合中,android:includeFontPadding=“false”
我遇到了同样的问题,这似乎是因为按钮的背景色。尝试将背景颜色更改为其他颜色,例如:
android:background="@color/colorActive"
看看它是否有效。然后,如果希望按钮使用,可以定义样式。要删除切换按钮周围的填充,我们需要设置minWidth和minHeight 0dp。
android:minWidth=“0dp”android:minHeight=“0dp”
这不是一个填充,而是背景的阴影,或者是
minHeight
和minWidth
的问题
如果您仍然想要良好的ripple效果,可以使用?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>
0dp
0dp
48dp
?属性/可选择项背景
并将其应用于按钮:
<Button
style="@style/Widget.AppTheme.MyCustomButton"
... />
为您的按钮提供自定义背景:@drawable/material\u btn\u blue您还可以使用布局\u宽度(高度)
参数来完成此操作
例如,我用android:layout_height=“18dp”代码删除了顶部和底部空间。标准按钮不应全宽使用,这就是为什么您会遇到这种情况 背景 如果您查看,您将看到一个按钮有一个48dp高度点击区域,但由于某种原因将显示为36dp高度 这是您看到的背景轮廓,它不会覆盖按钮本身的整个区域。
它有圆角和一些填充物,应该可以自己点击,包装内容,而不是覆盖屏幕底部的整个宽度 解决方案 如上所述,您需要的是不同的背景。不是一个标准的按钮,而是一个背景,可选择的项目与这个很好的涟漪效应 对于这个用例,有一个
?selectableItemBackground
主题属性,可以用于背景(尤其是在列表中)。它将添加一个平台标准ripple(或<21上的一些颜色状态列表),并将使用您当前的主题颜色 对于您的用例,您可以只使用以下内容:
<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" />
如果您的视图是唯一一个且跨越整个屏幕,则也不需要添加布局权重
如果你对你的背景应该是什么样子有不同的想法,你必须自己创建一个定制的可绘制的,并在那里管理颜色和状态
这个答案抄袭自
我的解决方案已将insetTop和InsettBottom属性设置为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"/>
经过数小时的反复研究,我终于找到了一个解决方案,它不需要使用不同的元素,也不需要操纵
最小高度
或最小宽度
,甚至不需要将按钮
环绕在相对长度
上
<Button
android:foreground="?attr/selectableItemBackground"
android:background="@color/whatever" />
这里的主要收获是前景的设置,而不是许多答案规定的背景。将背景本身更改为selectableItemBackground
只会覆盖您对按钮颜色/背景所做的任何更改(如果有)
更改前景可以让您两全其美:去掉“不可见”的填充,保留按钮的设计。您可以在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" />
移除顶部和底部衬垫
<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"/>
对于MaterialButton,添加以下属性,它将完美工作
<android.support.design.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:insetTop="0dp"
android:insetBottom="0dp"/>
花了一段时间,直到我意识到我不是在处理一些奇怪的填充行为,而是在处理minHeight。我认为这个答案根本没有回答最初的问题。但我还是很高兴我遇到了它,因为我不明白为什么我的按钮
在设置了android:background=“@null”
之后仍然占据了这么多空间。按钮的默认样式是一个很好的提示。使用android:minHeight=“0dp”android:minWidth=“0dp”这两个按钮都很重要,而不仅仅是其中一个。这将消除android牛轧糖上按钮的圆角和涟漪效应,知道是什么导致了这种行为吗?这个答案解决了一个GridLayout的问题,其中包含了文本上带有自动调整大小选项的按钮,当我试图让更多的按钮适应行/列时,这些按钮变得疯狂。我花了很长时间才找到的简单答案…我找到了。没有消失,呵呵
<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"/>
<android.support.design.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:insetTop="0dp"
android:insetBottom="0dp"/>