Android開發(fā)教程之shape和selector的結(jié)合使用
shape和selector是Android UI設(shè)計(jì)中經(jīng)常用到的,比如我們要自定義一個(gè)圓角Button,點(diǎn)擊Button有些效果的變化,就要用到shape和selector??梢赃@樣說,shape和selector在美化控件中的作用是至關(guān)重要的。
1.Shape
簡(jiǎn)介
作用:XML中定義的幾何形狀
位置:res/drawable/文件的名稱.xml
使用的方法:
Java代碼中:R.drawable.文件的名稱
XML中:android:background="@drawable/文件的名稱"
屬性:
<shape> android:shape=["rectangle" | "oval" | "line" | "ring"]
其中rectagle矩形,oval橢圓,line水平直線,ring環(huán)形
<shape>中子節(jié)點(diǎn)的常用屬性:
<gradient> 漸變
android:startColor 起始顏色
android:endColor 結(jié)束顏色
android:angle 漸變角度,0從上到下,90表示從左到右,數(shù)值為45的整數(shù)倍默認(rèn)為0;
android:type 漸變的樣式 liner線性漸變 radial環(huán)形漸變 sweep
<solid > 填充
android:color 填充的顏色
<stroke > 描邊
android:width 描邊的寬度
android:color 描邊的顏色
android:dashWidth 表示'-'橫線的寬度
android:dashGap 表示'-'橫線之間的距離
<corners > 圓角
android:radius 圓角的半徑 值越大角越圓
android:topRightRadius 右上圓角半徑
android:bottomLeftRadius 右下圓角角半徑
android:topLeftRadius 左上圓角半徑
android:bottomRightRadius 左下圓角半徑
2.Selector
簡(jiǎn)介
位置: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"> <!-- 默認(rèn)時(shí)的背景圖片--> <item android:drawable="@drawable/pic1" /> <!-- 沒有焦點(diǎn)時(shí)的背景圖片 --> <item android:state_window_focused="false" android:drawable="@drawable/pic_blue" /> <!-- 非觸摸模式下獲得焦點(diǎn)并單擊時(shí)的背景圖片 --> <item android:state_focused="true" android:state_pressed="true" android:drawable= "@drawable/pic_red" /> <!-- 觸摸模式下單擊時(shí)的背景圖片--> <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/pic_pink" /> <!--選中時(shí)的圖片背景--> <item android:state_selected="true" android:drawable="@drawable/pic_orange" /> <!--獲得焦點(diǎn)時(shí)的圖片背景--> <item android:state_focused="true" android:drawable="@drawable/pic_green" /> </selector>
第一個(gè)例子:圓角的Button
http://liangruijun.blog.51cto.com/3061169/630051
第二個(gè)例子:shape+selector綜合使用的例子 漂亮的ListView
先看看這個(gè)例子的結(jié)構(gòu):
selector.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="true"> <shape> <gradient android:angle="270" android:endColor="#99BD4C" android:startColor="#A5D245" /> <size android:height="60dp" android:width="320dp" /> <corners android:radius="8dp" /> </shape> </item> <item android:state_pressed="true"> <shape> <gradient android:angle="270" android:endColor="#99BD4C" android:startColor="#A5D245"/> <size android:height="60dp" android:width="320dp" /> <corners android:radius="8dp" /> </shape> </item> <item> <shape> <gradient android:angle="270" android:endColor="#A8C3B0" android:startColor="#C6CFCE" /> <size android:height="60dp" android:width="320dp" /> <corners android:radius="8dp" /> </shape> </item> </selector>
list_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/selector" > <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="20dp" /> <TextView android:text="data" android:id="@+id/title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" android:textSize="14sp" android:textStyle="bold" android:textColor="@color/black" > </TextView> </LinearLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#253853" > <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:cacheColorHint="#00000000" android:divider="#2A4562" android:dividerHeight="3px" android:listSelector="#264365" android:drawSelectorOnTop="false" > </ListView> </LinearLayout>
colors.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="white">#FFFFFFFF</color> <color name="transparency">#00000000</color> <color name="title_bg">#1C86EE</color> <color name="end_color">#A0cfef83</color> <color name="black">#464646</color> </resources>
MainActivity.xml
package com.lingdududu.customlist; import java.util.ArrayList; import java.util.HashMap; import xb.customlist.R; import android.R.array; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.SimpleAdapter; public class MainActivity extends Activity { ListView list; String data[] = new String[]{ "China","UK","USA","Japan","German","Canada","ET","Narotu" }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); list =(ListView) findViewById(R.id.list); SimpleAdapter adapter = new SimpleAdapter(this, getData(), R.layout.list_item, new String[]{"title","img"}, new int[]{R.id.title,R.id.img}); list.setAdapter(adapter); } private ArrayList<HashMap<String, Object>> getData() { ArrayList<HashMap<String, Object>> dlist = new ArrayList<HashMap<String,Object>>(); for(int i =0;i<data.length;i++){ HashMap<String, Object>map = new HashMap<String, Object>(); map.put("title", data[i]); map.put("img", R.drawable.item_left2); dlist.add(map); } return dlist; } }
效果圖:
以上所述是小編給大家分享的Android開發(fā)教程之shape和selector的結(jié)合使用的相關(guān)內(nèi)容,希望對(duì)大家有所幫助。
- Android編程使用自定義shape實(shí)現(xiàn)shadow陰影效果的方法
- Android Selector和Shape的使用方法
- Android自定義shape的使用示例
- Android控件系列之Shape使用方法
- Android中的Shape和Selector的結(jié)合使用實(shí)例
- Android Shape控件美化實(shí)現(xiàn)代碼
- Android shape和selector 結(jié)合使用實(shí)例代碼
- Android中shape定義控件的使用
- Android中drawable使用Shape資源
- 三款A(yù)ndroid炫酷Loading動(dòng)畫組件推薦
- Android開發(fā)之WebView組件的使用解析
- Android使用shape使組件呈現(xiàn)出特殊效果的方法
相關(guān)文章
android項(xiàng)目手機(jī)衛(wèi)士來電顯示號(hào)碼歸屬地
由于詐騙電話越來越猖狂,號(hào)碼歸屬地顯示越來越重要,本篇文章主要介紹了android手機(jī)衛(wèi)士來電顯示號(hào)碼歸屬地,有要的朋友可以了解一下。2016-10-10詳解Android Service與Activity之間通信的幾種方式
這篇文章主要介紹了詳解Android Service與Activity之間通信的幾種方式,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04通過Android trace文件分析死鎖ANR實(shí)例過程
遇到ANR(Application Not Responding)是比較常見的問題,產(chǎn)生ANR的原因有很多,比如CPU使用過高、事件沒有得到及時(shí)的響應(yīng)、死鎖等,下面將通過一次因?yàn)樗梨i導(dǎo)致的ANR問題,來說明如何通過trace文件分析ANR問題2013-06-06android長(zhǎng)截屏原理及實(shí)現(xiàn)代碼
本篇文章主要介紹了android長(zhǎng)截屏原理及實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08Android的ImageButton當(dāng)顯示Drawable圖片時(shí)就不顯示文字
Android提供的ImageButton當(dāng)顯示Drawable圖片時(shí)就不會(huì)再顯示文字了,下面與大家分享下3種解決方法,不會(huì)的朋友可以了解下哈2013-06-06Android 仿余額寶數(shù)字跳動(dòng)動(dòng)畫效果完整代碼
這篇文章主要介紹了Android 仿余額寶數(shù)字跳動(dòng)動(dòng)畫效果完整代碼,需要的朋友可以參考下2017-11-11Android開發(fā)筆記之:深入理解Cursor相關(guān)的性能問題
本篇文章是對(duì)Android中Cursor相關(guān)的性能問題進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05Android使用AlarmManager設(shè)置鬧鐘功能
這篇文章主要為大家詳細(xì)介紹了Android使用AlarmManager設(shè)置鬧鐘功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09