活用shape、selector和layer-list來打造自己想要的背景效果


活用shape、selector和layer-list來打造自己想要的背景效果

目錄(?)[+]

我們都知道,Android中 一些控件默認的背景都比較難看,所以在大部分情況下,都需要我們自己用<shape>來進行一些美化效果,比如給button加個圓角,邊線 之類的。當然假如想在點擊的時候給一些反饋,我們還需要用到<selector>,再復雜一些的可能還需要我們用layer-list進行層 疊來實現。

下面我先說一下它們的簡單用法(用法是網上找的,如有雷同,可能不是巧合~哈哈):

1.Shape
簡介
         作用:XML中定義的幾何形狀

Java代碼中:R.drawable.文件的名稱

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

<shape>中子節點的常用屬性:

<gradient>  漸變

Android:startColor  

Android:endColor  

結束顏色             

Android:angle  

Android:type  

<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" 

根據控件不同的選定狀態來定義不同的顯示效果

android:state_selected 是選中

android:state_focused 是獲得焦點

android:state_pressed 是點擊

android:state_checked 也是選中一般針對checkbox和radiobutton

 

android:state_window_focused 默認時的背景圖片
引用位置:res/drawable/文件的名稱.xml

使用的方法:
Java代碼中:R.drawable.文件的名稱
XML中:Android:background="@drawable/文件的名稱"
3.layer-list   
簡介:可以將多個圖片或上面兩種效果按照順序層疊起來,效果其實就和FrameLayout一樣

用法其實都不難,關鍵是我們要學會靈活的運用到實際中,下面我舉兩個例子,來看一下具體怎么用。

1)實現效果如下:


效果比較常見,加減按鈕在不點擊的時候是黑色的,點擊的時候變成橙色的。

我們先分析一下怎么做:

首先我們需要一個線性布局,里面放三個TextView(別的控件也可以)和兩個view(黑色分割線),控件安排好了,接下來我們繼續分析,最外 層的線性布局我們需要給弄一個圓角的效果,這個我們用<shape>就可以很容易做到,但是左邊的TextView好像有點復雜,因為它既需 要圓角(左上和左下)又需要添加圖片背景(減號)還需要根據狀態發生變化,右邊的也相同。那該怎么辦呢,下面我貼一下代碼:

布局中是這樣子的:

<LinearLayout
 android:layout_width="match_parent"  android:layout_height="50dp"  android:background="@drawable/shape_linear"  android:gravity="center_vertical"  android:orientation="horizontal"> <TextView  android:id="@+id/subtract"  android:layout_width="0dp"  android:layout_height="match_parent"  android:layout_weight="1"  android:background="@drawable/tv_add"  android:gravity="center" /> <View  android:layout_width="1dp"  android:layout_height="match_parent"  android:background="#000000" /> <TextView  android:layout_width="0dp"  android:layout_height="match_parent"  android:layout_weight="2"  android:gravity="center"  android:text="數量" /> <View  android:layout_width="1dp"  android:layout_height="match_parent"  android:background="#000000" /> <TextView  android:id="@+id/add"  android:layout_width="0dp"  android:layout_height="match_parent"  android:layout_weight="1"  android:background="@drawable/tv_subtract"  android:gravity="center"  android:selectAllOnFocus="true" /> </LinearLayout>

下面我們看一下shape_linear.xml、tv_subtract.xml、tv_add.xml:

shape_linear.xml:

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#ffffff" /> <corners android:radius="5dp" /> <!--邊線-->  <stroke  android:width="1dp"  android:color="#000000" /> </shape>

tv_subtract.xml:

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:state_pressed="true">
<bitmap android:src="@drawable/subtract_selected" android:gravity="center"/> <shape > <corners android:topLeftRadius="5dp" android:bottomLeftRadius="5dp"/> </shape> </item> <item> <bitmap android:src="@drawable/subtract_no_select" android:gravity="center"/> <shape > <corners android:topLeftRadius="5dp" android:bottomLeftRadius="5dp"/> </shape> </item></selector>

tv_add.xml:

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_pressed="true">
<bitmap android:src="@drawable/add_selected" android:gravity="center"/> <shape > <corners android:topRightRadius="5dp" android:bottomRightRadius="5dp"/> </shape> </item> <item> <bitmap android:src="@drawable/add_no_select" android:gravity="center"/> <shape > <corners android:topRightRadius="5dp" android:bottomRightRadius="5dp"/> </shape> </item></selector>

2)實現效果如下:


其實在這我就是舉個例子,估計這樣的需求不會有,上面要的結果是背景只顯示三條邊線,我們都知道<shape>的stroke屬性就是 來設置邊線的,可以它卻沒有辦法針對特定的邊來操作。那么這個應該怎么來實現呢,別忘了我們還有layer-list呢。我們都知道layer-list 其實就是將圖層疊起來,想象一下,我們將一張大的圖層放在第一層,然后我們再放一張小一點的圖層蓋在上面,那么第一層的邊緣部分是不是還會漏在外面。好 了,看代碼:

border_three.xml

<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <!--邊線的顏色-->  <item> <shape> <solid android:color="#ff0000" /> </shape> </item> <!--top left right 是偏移量-->  <!--主體的顏色-->  <item  android:left="1dp"  android:right="1dp"  android:top="1dp"> <shape> <solid android:color="#dcdcdc" /> </shape> </item> </layer-list>

假如現在有個需求,是讓你給按鈕背景添加個陰影效果是不是也有思路了~


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM