android 背景resource资源文件使用整理

效果图如下:

upload_avatar.xml布局文件代码

        <LinearLayout
            android:orientation="vertical"
            android:gravity="center"
            android:layout_marginBottom="20dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <Button
                android:text="立即拍照"
                android:textColor="#ffffff"
                android:textSize="13dp"
                android:layout_width="235dp"
                android:layout_height="40dp"
                android:background="@drawable/background_button_div"
                android:id="@+id/take_photo_inst"
                android:layout_marginBottom="15dp"
                style="?android:attr/borderlessButtonStyle"
                />
            <Button
                android:text="相册上传"
                android:textColor="#fda648"
                android:textSize="13dp"
                android:layout_width="235dp"
                android:layout_height="40dp"
                android:background="@drawable/background_button_div1"
                android:id="@+id/ok_button"
                style="?android:attr/borderlessButtonStyle"
                />
        </LinearLayout>

注意:  style="?android:attr/borderlessButtonStyle"

设置该属性就可以去掉Button自带的阴影。 
该属性在API 11及以上可用。鉴于现在普遍都是API 14,所以可以放心使用。已在5.1和6.0上试过,没问题。

background_button_div.xml 布局文件代码
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item  android:state_pressed="false">
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <solid android:color="#fda648" />
            <!--设置圆角 注意:    bottomRightRadius是左下角而不是右下角  bottomLeftRadius右下角 -->
            <corners  android:radius="5dip" />
            <padding
                android:left="10dp"
                android:right="10dp"
                android:top="10dp"
                android:bottom="10dp"/>
        </shape>
    </item>
    <item  android:state_pressed="true" >
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <solid android:color="#bbbbbb" />
            <!--设置圆角 注意:    bottomRightRadius是左下角而不是右下角  bottomLeftRadius右下角 -->
            <corners  android:radius="10dip" />

            <padding
                android:left="10dp"
                android:right="10dp"
                android:top="10dp"
                android:bottom="10dp"/>
        </shape>
    </item>
    
</selector>
background_button_div1.xml 布局文件代码
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item  android:state_pressed="false">
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <stroke
                android:width="1dp"
                android:color="#fda648">
            </stroke>
            <!--设置圆角 注意:    bottomRightRadius是左下角而不是右下角  bottomLeftRadius右下角-->
            <corners  android:radius="5dip" />

            <padding
                android:left="1dp"
                android:right="1dp"
                android:top="1dp"
                android:bottom="1dp"/>
        </shape>
    </item>
    <item  android:state_pressed="true" >
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <stroke android:color="#bbbbbb" />
            <!--设置圆角注意:    bottomRightRadius是左下角而不是右下角  bottomLeftRadius右下角-->
            <corners  android:radius="10dip" />
            <padding
                android:left="10dp"
                android:right="10dp"
                android:top="10dp"
                android:bottom="10dp"/>
        </shape>
    </item>

</selector>

ok,到此就结束了。下面整理下具体使用到的知识点:

1.Shape
简介

作用:XML中定义的几何形状

位置:res/drawable/文件的名称.xml

使用的方法:

Java代码中:R.drawable.文件的名称

XML中:Android:background="@drawable/文件的名称"

属性:

<shape>  Android:shape=["rectangle" | "oval" | "line" | "ring"]

其中rectagle矩形,oval椭圆,line水平直线,ring环形

<shape>中子节点的常用属性:

<gradient>  渐变

Android:startColor  

起始颜色

Android:endColor  

结束颜色             

Android:angle  

渐变角度,0从左到右,90表示从下到上,数值为45的整数倍,默认为0;

Android:type  

渐变的样式 liner线性渐变 radial环形渐变 sweep

<solid >  内部填充

Android:color  

填充的颜色

<stroke >描边

Android:width 

描边的宽度

Android:color 

描边的颜色

Android:dashWidth

 表示'-'横线的宽度

Android:dashGap 

表示'-'横线之间的距离

<corners >圆角

Android:radius  

圆角的半径 值越大角越圆

Android:topRightRadius  

右上圆角半径

Android:bottomLeftRadius 

右下圆角角半径

Android:topLeftRadius 

左上圆角半径

Android:bottomRightRadius 

左下圆角半径

<padding >边界填充

android:bottom="1.0dip" 

底部填充

android:left="1.0dip" 

左边填充

android:right="1.0dip" 

右边填充

android:top="0.0dip" 

上面填充

2.Selector
简介

根据不同的选定状态来定义不同的现实效果

分为四大属性:

android:state_selected 是否选中

android:state_focused 是否获得焦点

android:state_pressed 是否按压

android:state_enabled 是否设置是否响应事件,指所有事件

另:

android:state_window_focused 默认时的背景图片

引用位置:res/drawable/文件的名称.xml

使用的方法:
Java代码中:R.drawable.文件的名称
XML中:Android:background="@drawable/文件的名称"
<?xml version="1.0" encoding="utf-8" ?>     
<selector xmlns:Android="http://schemas.android.com/apk/res/android">   
<!-- 默认时的背景图片-->    
<item Android:drawable="@drawable/pic1" />      
<!-- 没有焦点时的背景图片 -->    
<item 
   Android:state_window_focused="false"      
   android:drawable="@drawable/pic_blue" 
   />     
<!-- 非触摸模式下获得焦点并单击时的背景图片 -->    
<item 
   Android:state_focused="true" 
   android:state_pressed="true"   
   android:drawable= "@drawable/pic_red" 
   />   
<!-- 触摸模式下单击时的背景图片-->    
<item 
   Android:state_focused="false" 
   Android:state_pressed="true"   
   Android:drawable="@drawable/pic_pink" 
   />    
<!--选中时的图片背景-->    
<item 
   Android:state_selected="true" 
   android:drawable="@drawable/pic_orange" 
   />     
<!--获得焦点时的图片背景-->    
<item 
   Android:state_focused="true" 
   Android:drawable="@drawable/pic_green" 
   />     
</selector> 
原文地址:https://www.cnblogs.com/zhang-cb/p/6495230.html