Shape and Selector are frequently used in Android UI design. For example, if we want to customize a rounded corner button and click the button to change some effects, we need to use shape and selector. It can be said that shape and Selector play a vital role in beautifying controls.
1. Shape
Introduction
Role: geometric shapes defined in XML
Location: Res/drawable/file name. xml
Usage:
In Java code: R. drawable. File Name
In XML: Android: Background = "@ drawable/file name"
Attribute:
<Shape> Android: Shape = ["rectangle" | "Oval" | "line" | "ring"] The rectagle rectangle, oval elliptic, line horizontal line, ring <shape> common attributes of neutron nodes: <gradient> gradient Android: startcolor starting color Android: endcolor ending color Android: angle gradient angle, 0 from top to bottom, 90 indicates that the value is from left to right, And the integer multiple of the value is 45 by default. Android: Type gradient style liner linear gradient Radial Ring gradient sweep <solid> fills Android: color fill color <stroke> stroke Android: width stroke width Android: Color stroke color Android: dashwidth represents the width of the '-' horizontal line Android: dashgap indicates the distance between '-' and <corners> rounded corner Android: radius rounded corner. The larger the radius is, the greater the angle is. Android: toprightradius: upper right corner radius Android: bottomleftradius: lower right corner radius Android: topleftradius top left corner radius Android: bottomrightradius bottom left corner radius
2. Selector
Introduction
Location: Res/drawable/file name. xml
Usage:
In Java code: R. drawable. File Name
In XML: Android: Background = "@ drawable/file name"
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
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" }; @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; } }
: