Android应用中res/drawable文件夹下定义的xml文件内容详解(一)

drawable文件夹下定义的xml有哪些作用?
首先,看看有哪些标签可以使用,一共有10类,我使用
Eclipse:Version: Luna Service Release 1a (4.4.1)。在xml中Ctrl+/可以看到以下内容。
这里写图片描述
还有一类layer-list,上图没有显示出来,暂时还不知道为什么,这个标签用于图形叠加。
从最常用和最熟悉的说起:

1、shape标签

2、使用示例

  • shape文件中stroke使用效果,定义了一个红色边框
    这里写图片描述
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- stroke就是定义的边框,这里指定边框宽度为1dip, -->
    <!-- 另外我们可以指定每个, -->
    <stroke
        android:width="1dip"
        android:color="@color/red" />
</shape>
  • 给TextView定义一个灰色背景,如果是纯色可以直接color,这里还是一试。另外试下shape文件中padding的用法。
    这里写图片描述
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- solid就一个颜色属性, -->
    <solid android:color="@color/gray" />
    <stroke
        android:width="2dp"
        android:color="@color/red" />
    <!--
         padding的作用时图形边框距内容的距离,padding值不包含边框宽度;这个属性应该用得比较少,因为控件本身就有类似控制属性。
        android:left    整型 左内边框距内容的距离
        android:top     整型 上内边框距内容的距离
        android:right   整型 右内边距
        android:bottom  整型 下内边距
    -->
    <padding android:left="20dp" />
</shape>
  • 3、给TextView定义一个白色背景,灰色圆角边框。
    这里写图片描述
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- 这里灰色边框 -->
    <stroke
        android:width="1dp"
        android:color="@color/grey" />
    <!-- 每个角半径,数字越大,角就越圆 -->
    <corners android:radius="10dp" />  
    <!-- 设置了左边距 -->
    <padding android:left="50dp"/>
</shape>
  • 4、给按钮定义一个从上到下渐变的背景色。
    这里写图片描述
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <corners android:radius="10dp" />
    <padding
        android:left="10dp"
        android:right="10dp" />
    <gradient
        android:angle="90"
        android:endColor="@color/backColor"
        android:startColor="@color/red" />
</shape>
  • 5、给按钮定义一个从上到下渐变的背景色并且点击时可以变化,。需要再定义一个点击时显示的背景色,还要用到selector
    这里写图片描述
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--默认时显示背景-->
    <item android:drawable="@drawable/shape_file_use_demo_4"/>
    <!--点击时显示背景-->
    <item android:drawable="@drawable/shape_file_use_demo_5" android:state_pressed="true"/>
</selector>
  • shape_file_use_demo_5.xml
    正常显示以第4示例中的背景来显示,点击时的背景人是调换了开始和结束的位置。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <corners android:radius="10dp" />
    <padding
        android:left="10dp"
        android:right="10dp" />
    <gradient
        android:angle="90"
        android:endColor="@color/red"
        android:startColor="@color/backColor" />
</shape>
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wolf犭良

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值