android drawable

概述

凡是要在屏幕上绘制的东西都可以叫drawable

shape drawable

使用shape drawable可以绘制圆形、长方形、梯形、线条等,XML drawable和屏幕像素密度无关,所以无需考虑创建特定像素密度目录,直接放入drawable文件夹就可以了

定义shape

button_beat_box_normal.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
     android:shape="oval"> <!-- 定义一个圆 -->
    <solid
        android:color="@color/dark_blue"/> <!-- 背景为深蓝色的圆形-->
</shape>

button_beat_box_pressed.xml

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

在样式中使用

    <style name="BeatBoxButton" parent="Widget.AppCompat.Button">
        <item name="android:background">@drawable/button_beat_box_normal</item>
    </style>

state list drawable

state list drawable 支持 按下、禁用、聚焦、激活等状态

创建state list drawable

button_beat_box.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_beat_box_pressed" android:state_pressed="true" />  <!--按下时-->
    <item android:drawable="@drawable/button_beat_box_normal" /> <!--正常时-->
</selector>

使用

 <style name="BeatBoxButton" parent="Widget.AppCompat.Button">
        <item name="android:background">@drawable/button_beat_box</item>
    </style>

layer list drawable

layer list drawable 能让多个drawable合而为一,

实例

定义一个红圈定,再义一个4dp粗粗的深红圈,这会产生一个暗红圈
定义
button_beat_box_pressed.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="oval">
            <solid android:color="@color/red" />
        </shape>
    </item>
    <item>
        <shape android:shape="oval">
            <stroke
                android:width="4dp"
                android:color="@color/dark_red"/>
        </shape>
    </item>
</layer-list>

使用

   <style name="BeatBoxButton" parent="Widget.AppCompat.Button">
        <item name="android:background">@drawable/button_beat_box</item>
    </style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值