使用shape画需要的简单的背景

shape(形状)

包含 矩形:rectangle、椭圆:oval、线:line、圆环:ring

一、矩形:

  1. solid:填充颜色

  2. corners:圆角大小,通过radius属性控制(topleftRadius: 左上。。。)

  3. padding:内边距

  4. stroke:边框    width:边框大小,color:颜色

  5. 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>

二、椭圆:

  1. size:大小  (height:高、width:宽)

  2. 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>

三、线:

  1. stroke:width:填充颜色的高度、dashGap:虚线间距高度、dashWidth:虚线高度

  2. 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>

四、圆环:

  1. 在shape标签中必须设置uselevel = “false”

  2. 在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

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值