Android中l(wèi)ayer-list基本使用詳解
使用layer-list可以將多個drawable按照順序?qū)盈B在一起顯示,默認情況下,所有的item中的drawable都會自動根據(jù)它附上view的大小而進行縮放,
layer-list中的item是按照順序從下往上疊加的,即先定義的item在下面,后面的依次往上面疊放
例子
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item > <shape android:shape="rectangle" > <solid android:color="#0000ff"/> </shape> </item> <item android:bottom="25dp" android:top="25dp" android:left="25dp" android:right="25dp"> <shape android:shape="rectangle" > <solid android:color="#00ff00" /> </shape> </item> <item android:bottom="50dp" android:top="50dp" android:left="50dp" android:right="50dp"> <shape android:shape="rectangle" > <solid android:color="#ff0000" /> </shape> </item> </layer-list>
布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="150dp" android:layout_height="150dp" android:background="@drawable/layer_list"/> </LinearLayout>
效果圖
紅色item最后定義在最上方,綠色item中間,最先定義藍色最下邊
這里設置了android:bottom="50dp" android:top="50dp" android:left="50dp" android:right="50dp"屬性
android:top="50dp";表示該item上邊以ImageView上邊界往里面縮了50dp
android:bottom="50dp"表示該item下邊以ImageView下邊界往里面縮了50dp
android:left="50dp";表示該item左邊以ImageView左邊界往里面縮了50dp
android:right="50dp";表示該item右邊以ImageView右邊界往里面縮了50dp
android:bottom="25dp" android:top="25dp" android:left="25dp" android:right="25dp"類似
layer-list給指定view實現(xiàn)三面邊框
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item > <shape android:shape="rectangle" > <solid android:color="#ff0000"/> </shape> </item> <item android:bottom="2dp" android:top="2dp" android:right="2dp"> <shape android:shape="rectangle" > <solid android:color="#ffffff" /> </shape> </item> </layer-list>
布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="150dp" android:layout_height="50dp" android:background="@drawable/border" android:layout_gravity="center" android:orientation="vertical" > </LinearLayout> </LinearLayout>
效果圖
實現(xiàn)帶陰影的按鈕效果:
代碼:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"> <!-- 灰色陰影 --> <layer-list> <item android:left="2dp" android:top="4dp"> <shape> <solid android:color="@android:color/darker_gray" /> <corners android:radius="4dp" /> </shape> </item> <!-- 紅色前景 --> <item android:bottom="4dp" android:right="2dp"> <shape> <solid android:color="#FF0000" /> <corners android:radius="4dp" /> </shape> </item> </layer-list> </item> <item> <!-- 灰色陰影 --> <layer-list> <item android:left="2dp" android:top="4dp"> <shape> <solid android:color="@android:color/darker_gray" /> <corners android:radius="4dp" /> </shape> </item> <!-- 白色前景 --> <item android:bottom="4dp" android:right="2dp"> <shape> <solid android:color="#FFFFFF" /> <corners android:radius="4dp" /> </shape> </item> </layer-list> </item> </selector>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android解析服務器端發(fā)來的xml數(shù)據(jù)示例
Android跟服務器交互數(shù)據(jù),有時數(shù)據(jù)量大時,就需要以xml形式的交互數(shù)據(jù),下面與大家分享下使用XmlPullParser來解析xml數(shù)據(jù),感興趣的朋友可以參考下哈2013-06-06Android自定義View的三個構(gòu)造函數(shù)
這篇文章主要介紹了Android自定義View的三個構(gòu)造函數(shù),需要的朋友可以參考下2017-06-06Android筆記之:onConfigurationChanged詳解
本篇是對Android中onConfigurationChanged的使用進行了詳細的分析介紹。需要的朋友參考下2013-05-05Android模擬實現(xiàn)網(wǎng)易新聞客戶端
這篇文章主要為大家詳細介紹了Android模擬實現(xiàn)網(wǎng)易新聞客戶端,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05圣誕節(jié),寫個程序練練手————Android 全界面懸浮按鈕實現(xiàn)
這篇文章主要介紹了圣誕節(jié),寫個程序練練手————Android 全界面懸浮按鈕實現(xiàn)的相關(guān)資料,需要的朋友可以參考下2015-12-12Android 兩個Fragment之間的跳轉(zhuǎn)和數(shù)據(jù)的傳遞實例詳解
這篇文章主要介紹了Android 兩個Fragment之間的跳轉(zhuǎn)和數(shù)據(jù)的傳遞實例詳解的相關(guān)資料,這里說明實現(xiàn)的思路及實現(xiàn)方法,需要的朋友可以參考下2017-07-07