美文网首页
震惊,没想到 Shape 也可以这么写

震惊,没想到 Shape 也可以这么写

作者: Android轮子哥 | 来源:发表于2021-07-19 00:04 被阅读0次
    • 你平时写 Shape 是不是这样滴
    • 第一步:先给 Shape 起个文件名:
    xxx_xxx_xxx_xxx.xml(得和项目中其他 Shape 文件名不冲突,得命名规范,又得符合使用场景,想个名字感觉好累)
    
    • 第二步:定义各种 Shape 属性
    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle"
        android:useLevel="true">
        
        <stroke android:color="#ffff0000"
            android:width="2dp"/>
        
        <solid android:color="#8000ff00" />
    
        <corners android:radius="20dp" />
        
    </shape>
    
    • 第三步:找到对应的 View 并设置
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@drawable/xxx_xxx_xxx_xxx"
        android:gravity="center"
        android:padding="10dp"
        android:text="圆角矩形-左右两边都是半圆弧-带边框"
        android:textColor="@android:color/black"
        android:textSize="14sp" />
    
    • 第四步:在布局中预览看看是不是自己想要的效果,有偏差的还得修改,改完还得再回来布局预览

    • 一个项目的开发最避不开写 Shape,其实写一个两个还好,如果是天天写,特别是在写的很多 Shape.xml 之后还没有复用的情况,你此刻的情绪是否是这样的?

    • 大家的苦,大家的痛,我都经历过,所以我都懂,这次我给大家带来一个新的框架,可以让大家写 Shape 的时候没有那么痛苦,我拿刚刚那个栗子,用一张图给大家看一下框架的效果
    • 没错的,聪明的你一定看明白了,是不是超级简单?我把 Shape 的属性定义搬到了布局中,这样做最明显的两个好处是:

      1. 降低写 Shape 时带来的麻烦,还能实时预览

      2. 减少项目中那些没有被重复利用 Shape 文件

    • 除此之外,框架的亮点有:

      1. 无学习成本:控件属性和原生 Shape 命名保持一致,无需额外学习

      2. 覆盖范围广:几乎涵盖所有常见的 View 控件,并且控件名称无任何记忆成本

    • 目前支持的控件有:

      • View:ShapeView、ShapeTextView、ShapeButton、ShapeImageView、ShapeRadioButton、ShapeCheckBox、ShapeEditText

      • Layout:ShapeLinearLayout、ShapeFrameLayout、ShapeRelativeLayout、ShapeConstraintLayout、ShapeRecyclerView

    • 这样的框架,你爱了吗?快来收藏使用吧!

    • 项目开源地址:Github码云

    Android 技术讨论 Q 群:10047167

    相关文章

      网友评论

          本文标题:震惊,没想到 Shape 也可以这么写

          本文链接:https://www.haomeiwen.com/subject/vuftmltx.html