日常开发中,我们会遇到一些Button、Textview…等控件的背景是圆角矩形、圆形…等,和android默认的控件背景矩形不一致,此时shape的作用就体现出来了,我们可以根据shape属性画出很多我们意想不到的背景图案,下面就简单介绍一下shape与textView的简单搭配。

首先介绍一些shape的属性:

1.shape的solid标签

用于指定边框填充颜色,也可以说是背景色
android:color:指定色值

2.shape的stroke标签

设置shape的外边界线
android:color边界线的颜色
android:width:边界线的宽度      
android:dashWidth: 段虚线的宽度(可以将边界线理解成一段段线无间隔的连接)
android:dashGap:段虚线的间隔

3.shape的padding标签

设置内容与边界的距离
android:left:左内边距
android:top:上内边距
android:right:右内边距
android:bottom:左内边距

4.shape的gradient标签

android:type:渐变的类型
linear:线性渐变,默认的渐变类型
radial:放射渐变,设置该项时,必须设置android:gradientRadius渐变半径属性
sweep:扫描性渐变  
android:angle:渐变的角度
android:startColor:渐变开始的颜色
android:centerColor:渐变中间的颜色
android:endColor:渐变结束的颜色
android:centerX:渐变中心的相对X坐标,放射渐变时(radial)才有效,在0.0到1.0之间,默认为0.5,表示在正中间  
android:centerY:渐变中心的相对X坐标,放射渐变时(radial才有效,在0.0到1.0之间,默认为0.5,表示在正中间
android:gradientRadius:渐变的半径,只有渐变类型为radial时才使用

5.shape的corners标签

android:radius:       四个角圆角
android:topLeftRadius:   左上角的圆角
android:topRightRadius:  右上角的圆角
android:bottomLeftRadius: 左下角的圆角
android:bottomRightRadiusleft:右下角的圆角

举例如下:

1.矩形边框TextView

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!--设置外边界线-->
    <stroke
        android:width="2px"
        android:color="#101010"
        ></stroke>
     <!--渐变-->
    <gradient
        android:angle="360"
        android:endColor="#DC2F2F"
        android:startColor="#FCD209" />
        <!--文字与矩形边框的边距-->
    <padding
        android:left="5dp"
        android:top="5dp"
        android:right="5dp"
        android:bottom="5dp"/>
</shape>

将TextView的blackground属性设置成上面的Drawable:

 <TextView
        android:layout_width="200dp"
        android:layout_height="64dp"
        android:layout_marginTop="20dp"
        android:text="矩形边框TextView"
        android:gravity="center"
        android:background="@drawable/juxing" /这里
        ></TextView>

效果图:
在这里插入图片描述

2.圆角矩形边框

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!--背景填充-->
<solid
    android:color="#70B520"
    ></solid>
<stroke
    android:color="#000000"
    android:width="2px"
    ></stroke>
    <corners
        android:bottomLeftRadius="20px"
        android:bottomRightRadius="20px"
        android:topLeftRadius="20px"
        android:topRightRadius="20px"
        ></corners>
    <padding
        android:bottom="5dp"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp" />
</shape>

效果图
在这里插入图片描述

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐