Android初级教程:shape的基本用法

转载本文请注明出处:http://blog.csdn.net/qq_32059827/article/details/52203347   点击打开链接

在自定义进度条之前,先来学习一下shape的用法。

一、在res目录下边新建一个drawble目录(如果您会自定义状态选择器的话,这将很简单)

二、新建一个android.xml文件,找到shape,点击finish

我们可以看到shape属性并不多,截图如下:


现在逐步介绍常用的属性用法:

1、<corners />表示  “角”,表示圆角

   (1)、radius : 表示半径android:radius="5dip"代表,指定圆角半径为5dip

2、<gradient />  :表示颜色渐变。放射性改变

测试上边两个属性特征:

在drawable/shapetest.xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <corners android:radius="5dip" >
    </corners>

    <gradient
        android:endColor="#00ff00"
        android:startColor="#ff0000" />

</shape>

在测试activity的布局文件引入shape.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:textSize="22sp"
        android:background="@drawable/shapetest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

此时显示效果图片:

好了。接着看shape的后边的几个属性:

3、<padding />表示内间距,与布局文件中的padding效果一样,就不做介绍。

4、<size />同上,可以直接在布局文件设置

5、<solid />固定颜色,设置这个原色后,放射颜色就不起作用

6、<stoke />改组件加边框线,看一下设置这个属性后,变为什么样子:

<stroke android:width="1dip"
        android:color="#0000ff"
        android:dashWidth="5dip"
        android:dashGap="5dip"/>

分别表示:外边框颜色宽度为1,颜色为蓝色,破折号长度为5,破折号之间的间距为5.效果截图如下:

到这里shape的基本用法就介绍完了。学习完shape基本用法,在Android简易实战系列第十七话中,完成一个自定义彩色进度条的案例

原文地址:https://www.cnblogs.com/wanghang/p/6299617.html