在AndroidUI设计中,大多数时候需要自定义组件圆角,如下:
当然,这也可以让UI设计师直接切图,作为背景使用。为了低耦合,这类简单的圆角设计,还是用代码比较方便。
首先在drawable文件下创建corner_edit.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
>
<solid android:color="#ffff"/>
<corners
android:topLeftRadius="10dp"
android:topRightRadius="10dp"
android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"/>
<stroke
android:width="1dp"
android:color="#000"/>
</shape>
solid的color属性用来指明矩形内填充的颜色,corner的属性分别代表左右上下四个圆角半径,在这里,四个角圆角半径一样可以使用radius属性,同样能达到圆角的效果。stroke用来描边的属性,上图的width是1dp,下图是10dp,color就是边的颜色。
shape标签,除了有矩形的圆角,还可以制作其他图形,它共有四个值:
line代表的是直线,oval代表的是椭圆,ring代表的是环形,默认的是rectangle矩形。
然后在activity_main.xml文件使用它:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="圆角设计"
android:background="@drawable/corner_edit"/>
<LinearLayout
android:layout_marginTop="15dp"
android:layout_marginLeft="15dp"
android:background="@drawable/corner"
android:layout_width="100dp"
android:layout_height="40dp">
<TextView
android:gravity="center"
android:layout_gravity="center"
android:layout_marginLeft="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="圆角设计"/>
</LinearLayout>
<EditText
android:layout_marginTop="15dp"
android:layout_marginLeft="15dp"
android:gravity="center"
android:layout_width="100dp"
android:layout_height="40dp"
android:background="@drawable/corner_edit"
android:text="圆角设计"/>
</LinearLayout>
至此,普通的圆角设计就完成了,标签和属性不多,也不复杂,简单方便。