android - 为什么 MaterialButton 文本不包含完整空间?

标签 android material-design

参见:

enter image description here

代码:

<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>

使用默认填充的结果。

enter image description here

和覆盖填充到 0dp 的结果 enter image description here

希望这对您有所帮助,祝您编码愉快!!

关于android - 为什么 MaterialButton 文本不包含完整空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58311382/

相关文章:

java - Fontawesome-fx jar 在场景生成器中没有 UI 组件

android - 为 Activity 中的特定 View 定义某些进入和退出转换

带有 Material 设计的Angularjs无法实例化模块ngMaterial

css - Material Design/Materialize 3列列表

ImageView中的Android动画被垂直拉伸(stretch)

android - 发送坐标到设备

php - 从数组输入数据时如何防止mysql中的重复输入?

android.mk 包含执行命令

css - 如何在 Angular 2 Material 的 md-grid-tile 中嵌套 md-card?

Android 线程 - 队列可运行