参见:
代码:
<RelativeLayout
android:id="@+id/circle_card_payment"
android:layout_width="40dp"
android:layout_height="40dp">
<com.google.android.material.button.MaterialButton
android:id="@+id/logo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:backgroundTint="@color/colorBlack"
android:insetLeft="0dp"
android:insetTop="0dp"
android:insetRight="0dp"
android:insetBottom="0dp"
android:text="W"
android:textColor="#FFFFFF"
android:textSize="16sp"
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay_ButtonCircle50"
app:strokeWidth="0dp" />
</RelativeLayout>
样式.xml
<style name="ShapeAppearanceOverlay_ButtonCircle50">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">20dp</item>
</style>
为了确认 Material 按钮包含完整空间,我将 Material 按钮颜色设置为黑色。内部文本颜色为白色。
有人可以根据我的情况给我确切的解决方案吗?请注意,您不应将相对布局尺寸从 40 dp 增加到 50 dp,我知道这可以解决问题,但 Logo 尺寸会更大,这是不合适的。
请注意,我必须将此按钮包装在任何根布局中。那是强制性的。就像现在我的 Material 按钮在相对布局内。
最佳答案
Why does MaterialButton text not containing full space?
问题出在 Material 按钮上,因为默认情况下 Material 按钮具有填充。您通过在 xml 本身中设置填充属性来删除或覆盖该填充,这应该可以解决问题。
只需添加 android:padding = "0dp"即可解决此问题。或作为 Ricardo.A 在样式 <item name="android:padding">0dp</item>
的评论集中建议
<RelativeLayout
android:id="@+id/circle_card_payment"
android:layout_width="40dp"
android:layout_height="40dp">
<com.google.android.material.button.MaterialButton
android:id="@+id/logo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:backgroundTint="#323232"
android:insetLeft="0dp"
android:insetTop="0dp"
android:insetRight="0dp"
android:padding="0dp"
android:insetBottom="0dp"
android:text="W"
android:textColor="#FFFFFF"
android:textSize="16sp"
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay_ButtonCircle50"
app:strokeWidth="0dp" />
</RelativeLayout>
使用默认填充的结果。
希望这对您有所帮助,祝您编码愉快!!
关于android - 为什么 MaterialButton 文本不包含完整空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58311382/