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

制作胶囊状按钮

核心是采用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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 均属于笔记,仅供个人参考,有问题欢迎指正 PopupWindow使用小结: * 1,点击外围、返回键、自身控件使弹...
    f44148db1e8c阅读 5,999评论 0 0
  • 很早看过这篇文章,并做了笔记,后来看到群里的小伙伴有问相关Drawable的问题,就把这篇翻译过来的文章给放出来了...
    Kotyo阅读 1,509评论 0 5
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,489评论 2 45
  • 图标平台 阿里图标库 Android学习论坛 Android巴士AndroidDevelopers Android...
    toro宇阅读 477评论 0 0
  • ----------------------Java规范---------------------- 源文件结构 ...
    pphdsny阅读 325评论 0 0