shape和selector是Android UI設計中常常用到的,好比咱們要自定義一個圓角Button,點擊Button有些效果的變化,就要用到shape和selector。能夠這樣說,shape和selector在美化控件中的做用是相當重要的。java
1.Shapeandroid
簡介app
做用:XML中定義的幾何形狀ide
位置:res/drawable/文件的名稱.xmlthis
使用的方法:spa
Java代碼中:R.drawable.文件的名稱設計
XML中:Android:background="@drawable/文件的名稱"xml
屬性:圖片
<shape> Android:shape=["rectangle" | "oval" | "line" | "ring"]utf-8
其中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 左下圓角半徑
2.Selector
簡介
位置: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>
第一個例子:圓角的Button
第二個例子:shape+selector綜合使用的例子 漂亮的ListView
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;
}
}