美文网首页Android开发Android开发
自定义控件之shape标签

自定义控件之shape标签

作者: dayang | 来源:发表于2016-11-29 17:05 被阅读91次

绘制shape.xml文件,shape其实是一个图形

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size
    android:width="40dp"
    android:height="40dp"/>
<solid android:color="#543422" />
<gradient
    android:angle="270"
    android:endColor="#44d900"
    android:startColor="#ff9d44" />
<!--边框设置虚线-->
<stroke
    android:width="2dp"
    android:color="#736292"
    android:dashGap="5dp"
    android:dashWidth="10dp"
/>
<corners android:radius="5dp" />
<padding
    android:bottom="20dp"
    android:left="20dp"
    android:right="20dp"
    android:top="20dp" />
</shape>

作为Button的背景图片和Button的上方图片

<Button
    android:drawableTop="@drawable/shape"
    android:text="shape"
    android:textColor="#000000"
    android:background="@drawable/shape"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"  />
shape效果图
Button_Shape.png
shape本身图形shape的属性

android:shape=["rectangle" | "oval" | "line" | "ring"]
图形设置有矩形(rectangle)椭圆形(oval)线性形状(line)环形(ring)

使用shape属性设置EditText的光标图形
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"><!--设置光标的图形 -->
<!--设置光标的大小 -->
<size android:width="2dp"/>
<!--设置光标的颜色 -->
<solid android:color="#000000"/></shape>
shape子标签
  • size

图形的大小

  • corner

圆角,指的是图形的四个角的弧度;
圆角android:radius为角的弧度,值越大角越圆。

  • solid

填充,可以理解为绘制图形的背景颜色;android:color指定填充的颜色

  • gradient

渐变,指的是填充的颜色效果;
起始颜色结束颜色android:startColor 和 android:endColor
渐变模式android:type="linear",即线性渐变,
径向渐变 android:type="radial"
渐变角度android:angle,必须为45的整数倍
渐变半径android:gradientRadius="50",渐变模式为径向渐变

  • stroke

边框,也指描边
**android:width="2dp" **边框的宽度
android:color 边框的颜色。
把边框设置成虚线:
**android:dashWidth="5dp" ** 虚线的宽度
**android:dashGap="3dp" **虚线与虚线的距离

  • padding

内边距
当作为某个控件的背景时,控制的是控件里的内容跟背景的距离

相关文章

网友评论

    本文标题:自定义控件之shape标签

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