shape(形状)
包含 矩形:rectangle、椭圆:oval、线:line、圆环:ring
一、矩形:
-
solid:填充颜色
-
corners:圆角大小,通过radius属性控制(topleftRadius: 左上。。。)
-
padding:内边距
-
stroke:边框 width:边框大小,color:颜色
-
gradient:渐变 startcolor:起始颜色、endcolor:终止颜色 angle:渐变角度(0-> 左到右 90-> 下到上 180->右到左 270->上到下)
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/colorPrimary"></solid>
<corners android:radius="10dp"></corners>
<padding android:bottom="12dp"
android:left="12dp"
android:right="12dp"
android:top="12dp"></padding>
<stroke android:width="5dp"
android:color="@color/colorAccent"></stroke>
<!--angle 渐变角度,0:左到右;90:下到上;180:右到左;270:上到下-->
<gradient android:startColor="@android:color/white"
android:endColor="@android:color/black"
android:angle="90"></gradient>
</shape>
二、椭圆:
-
size:大小 (height:高、width:宽)
-
gradient:渐变 (centerX:渐变的X轴起始位置,范围为0~1,0.5为圆心。centerY 一样。type:linear线性渐变、sweep扫描性渐变、radial放射性渐变--必须同时设置gradientRadius属性)
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="oval"
xmlns:android="http://schemas.android.com/apk/res/android">
<!--<solid android:color="@color/colorAccent"></solid>-->
<padding android:top="36dp"
android:left="12dp"
android:right="12dp"
android:bottom="0dp"></padding>
<size android:width="100dp"
android:height="100dp"></size>
<gradient android:centerX="0.7"
android:centerY="0.7"
android:type="radial"
android:gradientRadius="50dp"
android:startColor="@color/colorAccent"
android:endColor="@color/colorPrimary"></gradient>
</shape>
三、线:
-
stroke:width:填充颜色的高度、dashGap:虚线间距高度、dashWidth:虚线高度
-
size:线的高度,必须大于填充颜色的高度
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="line"
xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="1dp"
android:color="@color/colorAccent"
android:dashGap="20dp"
android:dashWidth="10dp"></stroke>
<size android:height="2dp"></size>
</shape>
四、圆环:
-
在shape标签中必须设置uselevel = “false”
-
在shape标签中设置thickness圆环宽度
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="ring"
android:useLevel="false"
android:thickness="10dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/colorAccent"></solid>
<gradient android:startColor="@color/colorAccent"
android:endColor="@color/colorPrimary"
android:type="linear"></gradient>
</shape>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:background="@drawable/shape"
android:textColor="@android:color/white"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:background="@drawable/oval"
android:textColor="@android:color/white"
/>
<LinearLayout
android:layout_marginTop="12dp"
android:background="@drawable/line"
android:layout_width="match_parent"
android:layout_height="wrap_content"></LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ring"/>
</LinearLayout>
矩形和椭圆可用background设置
线则用LinearLayout
圆环用ImageView