按钮边框不显示

t9aqgxwy  于 2021-08-20  发布在  Java
关注(0)|答案(4)|浏览(325)

我尝试在android中为按钮添加边框,但不幸的是,我看不到按钮边框。
可拉伸锉刀👇

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="12dp" />
            <stroke android:width="3px" android:color="#7E8082" />
        </shape>
    </item>
</selector>

xml👇

<Button
        android:id="@+id/btn_signup"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_marginTop="122dp"
        android:background="@drawable/bg_signup"
        android:fontFamily="@font/poppins_semibold"
        android:text="Create an Account"
        android:textAllCaps="false"
        android:textColor="@color/black"
        app:layout_constraintTop_toBottomOf="@+id/btn_login" />

bnl4lu3b

bnl4lu3b1#

您不需要使用自定义的绘图工具,只需使用 MaterialButton :

<com.google.android.material.button.MaterialButton
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           style="@style/Widget.MaterialComponents.Button.OutlinedButton"
           app:strokeColor="#7E8082"
           app:strokeWidth="1dp"
           app:cornerRadius="12dp"/>

u5i3ibmn

u5i3ibmn2#

用下面的代码替换可绘制文件。如果你想让broder更清晰可见,增加stroker部分的宽度。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <corners
        android:topLeftRadius="5dp"
        android:topRightRadius="5dp"
        android:bottomLeftRadius="5dp"
        android:bottomRightRadius="5dp"
        />
    <stroke
        android:width="2dp"
        android:color="#cdcdcd"
        />
</shape>
iibxawm4

iibxawm43#

不要将选择器用于单个选项 shape 直接地
bg_注册

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#cdcdcd"/>
    <corners
        android:radius="4dp"
    />
    <stroke
        android:width="1dp"
        android:color="#000"
    />
</shape>
wfauudbj

wfauudbj4#

使用样式属性而不是 android:background="@drawable/bg_signup" 创建一个自定义样式,然后调用它 android:style="@style/bg_signup"

相关问题