移除边界和按钮边框之间的空白间隔

8
我该如何去除Button边框和视图边界之间的空白?尝试将padding设置为0dp,但没有效果。
这是默认样式的材料按钮。

enter image description here enter image description here


你的按钮背景drawable是什么?是自定义的吗? - pskink
不使用 Material Components 库中的 Widget.MaterialComponents.Button 主题。 - Nabeel
1
你能分享一些代码吗? - VasFou
所以请检查它是如何定义的 - 很可能你的“padding”就是在那里定义的。 - pskink
<Button android:id="@+id/sign_in_link" style="@style/TextButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="24dp" android:fontFamily="sans-serif" android:text="@string/sign_in_link_label" android:textAllCaps="false" android:textColor="#555" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/sign_up_btn" /> - Nabeel
显示剩余6条评论
2个回答

14

MaterialButton 具有默认样式和这些插页:

 <style name="Widget.MaterialComponents.Button"..>
    <item name="android:insetLeft">0dp</item>
    <item name="android:insetRight">0dp</item>
    <item name="android:insetTop">@dimen/mtrl_btn_inset</item>
    <item name="android:insetBottom">@dimen/mtrl_btn_inset</item>
    ...
 </style>

@dimen/mtrl_btn_inset的值为6dp

直接使用即可:

  <com.google.android.material.button.MaterialButton
      android:insetTop="0dp"
      android:insetBottom="0dp"
      ../>

这里输入图片描述这里输入图片描述


我可以在样式中将minHeight更改为36dp吗? - Nabeel
@nabeel 是的。您可以在您的样式中添加 <item name="android:minHeight">36dip</item> 或者在您的按钮中添加 android:minHeight="36dip"。默认值是 48dp - Gabriele Mariotti
默认情况下,使用插图和48dp的最小高度,因为这是使按钮对大多数用户可访问的最小触摸目标大小。如果将高度缩小,可能会使您的应用程序更难使用。https://www.w3.org/WAI/WCAG21/Understanding/target-size.html - Cameron Ketcham

1
您可以使用权重并将宽度设置为0dp,这样间距就会被去除。

我正在使用ConstraintLayout。 - Nabeel
ConstraintLayout将是您的父布局,但如果您想要水平放置按钮,则需要使用LinearLayout和按钮。 - rachna
在ConstraintLayout中,您可以使用以下内容:layout_constraintBottom_toBottomOf,layout_constraintBottom_toEndOf,app:layout_constraintLeft_toRightOf。 - rachna
请查看我上传的另一张图片 - Nabeel
你是否使用了 app:layout_constraintTop_toBottomOf?它用于当你想让第一个按钮与另一个按钮平行时。 - rachna

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接