安卓开发

如何在Android中制作胶囊形状按钮

2021-10-08  本文已影响0人  蓝不蓝编程

制作胶囊状按钮

核心是采用shape来进行绘制。
可以作为View、TextView、各种Layout的背景(background)属性,但是不能作为Button的背景。
样例:



代码:shape_capsule_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <corners
        android:bottomLeftRadius="30dp"
        android:bottomRightRadius="30dp"
        android:topLeftRadius="30dp"
        android:topRightRadius="30dp" />

    <solid android:color="#cdcdcd" />
</shape>

使用:

<TextView
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@drawable/shape_capsule_bg"
    android:gravity="center"
    android:text="获取验证码"
    android:textColor="#ffffff"
    android:textSize="16sp" />

半个胶囊效果及实现方式

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <corners
        android:bottomLeftRadius="30dp"
        android:bottomRightRadius="0dp"
        android:topLeftRadius="30dp"
        android:topRightRadius="0dp" />

    <solid android:color="#cdcdcd" />

</shape>

参考资料:
https://www.it1352.com/1910431.html

上一篇 下一篇

猜你喜欢

热点阅读