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

问题描述 投票:0回答:1

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

enter image description here

创建房间(btn_create_room_bg):

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape
            android:shape="rectangle">
            <corners
                android:bottomLeftRadius="360dp"
                android:topLeftRadius="360dp" />
            <solid android:color="@color/colorAccent" />
        </shape>
    </item>
</layer-list>

加入房间(btn_join_room_bg):

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <corners
                android:bottomRightRadius="360dp"
                android:topRightRadius="360dp" />
            <solid android:color="@color/colorPrimary" />
        </shape>
    </item>
</layer-list>

按钮代码:这是我的线性布局的两键代码。

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

我需要这样的输出。

enter image description here

android android-studio android-layout android-button
1个回答
0
投票

您可以使用CornerTreatment建立自定义MaterialButton

您可以扩展默认的CutCornerTreatment

CutCornerTreatment

然后只需将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)); } } 应用于CornerTreatment

Button

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());

仅关于enter image description here的注释:在new RelativeCornerSize(0.5f)中已更改。在1.2.0-beta01之前。

© www.soinside.com 2019 - 2024. All rights reserved.