gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 09:42:50 25 4
gpt4 key购买 nike

我正在尝试为我的按钮(简单)制作自定义背景,但它显示不正确。它只显示以下输出。

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

最佳答案

您可以构建自定义 CornerTreatment MaterialButton .

您可以扩展默认 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));
}
}

然后只需应用 CornerTreatmentButton :
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中改变了.之前是 new RelativeCornerSize(50)) .

关于android - 如何创建自定义形状按钮背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62255460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com