Android 如何创建自定义形状按钮背景?

Android 如何创建自定义形状按钮背景?,android,android-studio,android-layout,android-button,Android,Android Studio,Android Layout,Android Button,我试图为我的按钮(简单)创建一个自定义背景,但它没有正确显示。它仅显示以下输出 创建房间(btn\u创建房间\u bg): 会客室(btn\U会客室\U bg): 按钮代码: 这是我在线性布局中的两个按钮代码 <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStar

我试图为我的按钮(简单)创建一个自定义背景,但它没有正确显示。它仅显示以下输出

创建房间(btn\u创建房间\u bg):


会客室(btn\U会客室\U bg):


按钮代码: 这是我在线性布局中的两个按钮代码

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="300dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="300dp"
        android:orientation="horizontal">

        <Button
            android:layout_width="0dp"
            android:layout_height="48dp"
            android:layout_marginEnd="8dp"
            android:layout_weight="5"
            android:background="@drawable/btn_create_room_bg"
            android:fontFamily="@font/open_sans_bold"
            android:text="@string/landing_btn_create_room"
            android:textAllCaps="false"
            android:textColor="@color/colorWhite" />

        <Button
            android:layout_width="0dp"
            android:layout_height="48dp"
            android:layout_marginStart="8dp"
            android:layout_weight="5"
            android:background="@drawable/btn_join_room_bg"
            android:fontFamily="@font/open_sans_bold"
            android:text="@string/landing_btn_join_room"
            android:textAllCaps="false"
            android:textColor="@color/colorWhite" />
    </LinearLayout>

我需要这样的输出


您可以使用
MaterialButton
创建自定义
角落处理

您可以扩展默认值:

然后只需将
CornerTreatment
应用于
按钮

MaterialButton materialButton = findViewById(R.id....);
FullCutCornerTreatment customCutCornerTreatment = new FullCutCornerTreatment();

materialButton.setShapeAppearanceModel(materialButton.getShapeAppearanceModel().toBuilder()
        //Standard rounded corner with corner radius=50%
        .setTopLeftCorner(CornerFamily.ROUNDED,new RelativeCornerSize(0.5f))
        .setBottomLeftCorner(CornerFamily.ROUNDED,new RelativeCornerSize(0.5f))
        //Square angle
        .setTopRightCorner(CornerFamily.ROUNDED,0)
        //Full cut corner
        .setBottomRightCorner(customCutCornerTreatment)
             .setBottomRightCornerSize(new RelativeCornerSize(0.5f))
        .build());


只需注意一下
新的RelativeCornerSize(0.5f)
:它在
1.2.0-beta01
中发生了变化。在它成为新的RelativeCornerSize(50)之前

请指定您正在使用的按钮类型(简单按钮或其他)?我正在使用简单按钮。添加代码以使其清晰。
 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="300dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="300dp"
        android:orientation="horizontal">

        <Button
            android:layout_width="0dp"
            android:layout_height="48dp"
            android:layout_marginEnd="8dp"
            android:layout_weight="5"
            android:background="@drawable/btn_create_room_bg"
            android:fontFamily="@font/open_sans_bold"
            android:text="@string/landing_btn_create_room"
            android:textAllCaps="false"
            android:textColor="@color/colorWhite" />

        <Button
            android:layout_width="0dp"
            android:layout_height="48dp"
            android:layout_marginStart="8dp"
            android:layout_weight="5"
            android:background="@drawable/btn_join_room_bg"
            android:fontFamily="@font/open_sans_bold"
            android:text="@string/landing_btn_join_room"
            android:textAllCaps="false"
            android:textColor="@color/colorWhite" />
    </LinearLayout>
public class FullCutCornerTreatment extends CutCornerTreatment {

    protected static final float ANGLE_LEFT = 180;

    public FullCutCornerTreatment() {
        super();
    }

    @Override
    public void getCornerPath(
            @NonNull ShapePath shapePath, float angle, float interpolation, float radius) {
        shapePath.reset(0, 2*radius * interpolation, ANGLE_LEFT, 180 - angle);
        shapePath.lineTo(
                (float) (Math.sin(Math.toRadians(angle)) * radius * interpolation),
                (float) (Math.sin(Math.toRadians(90 - angle)) * radius * interpolation));
    }
}
MaterialButton materialButton = findViewById(R.id....);
FullCutCornerTreatment customCutCornerTreatment = new FullCutCornerTreatment();

materialButton.setShapeAppearanceModel(materialButton.getShapeAppearanceModel().toBuilder()
        //Standard rounded corner with corner radius=50%
        .setTopLeftCorner(CornerFamily.ROUNDED,new RelativeCornerSize(0.5f))
        .setBottomLeftCorner(CornerFamily.ROUNDED,new RelativeCornerSize(0.5f))
        //Square angle
        .setTopRightCorner(CornerFamily.ROUNDED,0)
        //Full cut corner
        .setBottomRightCorner(customCutCornerTreatment)
             .setBottomRightCornerSize(new RelativeCornerSize(0.5f))
        .build());