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

时间:2020-06-08 05:16:47

标签: android android-studio android-layout android-button

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

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

1 个答案:

答案 0 :(得分:2)

您可以使用from django.contrib.auth.models import User from django.http import JsonResponse, HttpResponse from .models import User from django.shortcuts import render, redirect from django.views.decorators.csrf import csrf_exempt from .serializers import FidoSerializer from django.contrib.auth import authenticate, login @csrf_exempt def signup(request): if request.method == 'GET': return render(request, 'useraccount/signup.html') @csrf_exempt def signup_sign(request): #일반유저 회원가입 if request.method == 'POST': username = request.POST['name'] userid = request.POST['id'] company = request.POST['company'] code = request.POST['code'] account = User.objects.create_user( username=username, userid = userid, company = company, companycode = code, ) #login(request, account) account.save() return render(request, 'useraccount/success.html') def ShowAllAccount(request): if request.method == 'GET': query_set = User.objects.all() #모든 데이터가져옴 serializer = FidoSerializer(query_set, many=True) # json형태로 변환 return JsonResponse(serializer.data, safe=False) def SearchAccount(request): if request.method == 'GET': return User.objects.filter(username='junbeom') @csrf_exempt def login(request): response_data = {} if request.method == 'GET': return render(request, 'useraccount/login.html') if request.method == 'POST': login_username = request.POST['name'] login_userid = request.POST['id'] login_usercompany = request.POST['company'] login_usercode = request.POST['code'] user = authenticate(request, username = login_username, userid=login_userid, company = login_usercompany, companycode=login_usercode) if user is not None: login(request, user) return render(request, 'useraccount/login_success.html') else: return render(request, 'useraccount/login_fail.html') 构建自定义 CornerTreatment

您可以扩展默认的CutCornerTreatment

MaterialButton

然后将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

enter image description here

只是关于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()); 的注释:它在new RelativeCornerSize(0.5f)中进行了更改。在1.2.0-beta01之前。