【问题标题】:Remove space between bounds and button border删除边界和按钮边框之间的空间
【发布时间】:2020-02-09 14:41:21
【问题描述】:

如何删除Button 边界和视图边界之间的空间尝试将padding 设置为0dp 但没有结果。
这些是具有默认样式的材质按钮。

【问题讨论】:

  • 你的按钮背景是什么?定制的?
  • 不使用材质组件库中的 Widget.MaterialComponents.Button 主题
  • 你能分享一些代码吗?
  • 所以检查它是如何定义的 - 很可能你的“填充”是在那里制作的
  • <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" />

标签: android material-design android-button material-components material-components-android


【解决方案1】:

您可以使用权重并将宽度设为 0dp,然后空间将被删除。

【讨论】:

  • b/w 我正在使用 ConstraintLayout
  • ConstraintLayout 将是您的父级,但如果您希望按钮处于水平状态,那么您将需要使用线性布局和按钮
  • 在 ConstraintLayout 中,您可以使用:layout_constraintBottom_toBottomOf,layout_constraintBottom_toEndOf,app:layout_constraintLeft_toRightOf 。
  • 请查看我上传的另一张图片
  • 你是否使用过 app:layout_constraintTop_toBottomOf,当你希望第一个按钮与另一个按钮平行时使用它
【解决方案2】:

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 是的。您可以添加您的样式&lt;item name="android:minHeight"&gt;36dip&lt;/item&gt; 或您的按钮android:minHeight="36dip"。默认为48dp
  • 默认使用 insets 和 48dp 最小高度,因为这是使大多数用户可以访问按钮的最小触摸目标尺寸。通过减小高度,您可能会使您的应用程序更难使用。 w3.org/WAI/WCAG21/Understanding/target-size.html
猜你喜欢
  • 2014-12-05
  • 1970-01-01
  • 2023-03-26
  • 2015-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-10
相关资源
最近更新 更多