首页 > 解决方案 > 当我自定义高度和自定义背景时,为什么按钮文本不是 wrap_content 并且消失了?

问题描述

当我自定义高度和自定义背景时,为什么按钮文本不是 wrap_content 并且消失了?

XML 代码:

<Button
            android:id="@+id/button_create"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:layout_marginEnd="8dp"
            android:background="@drawable/bg_button_3"
            android:text="@string/create"
            android:theme="@style/FontTheme1"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

在此处输入图像描述

标签: javaandroidxmlbuttonandroid-button

解决方案


你的问题是身高。利用:

android:layout_height="wrap_content"

在任何情况下,您都不需要自定义背景来获得圆角。
只需使用:

<com.google.android.material.button.MaterialButton
   ....
   style="@style/Widget.MaterialComponents.Button"
   app:cornerRadius=".."
    .../>

推荐阅读