Android shape and Selector use instance code _android

Source: Internet
Author: User
Tags touch

Shape and selector are often used in Android UI design, such as we want to customize a fillet button, click on the button some effect changes, we need to use shape and selector. It can be said that the role of shape and selector in beautifying the control is essential.

1.Shape

Brief introduction

function: Geometric shapes defined in XML

Location:res/drawable/The name of the file. xml

Methods to use:

in Java code: R.drawable. The name of the file

xml:android:background= "@drawable/File name"

Property:

<shape> android:shape=["Rectangle" | "Oval" | "Line" | "Ring"]

One of the rectagle rectangles, oval ellipses, line horizontal lines, ring rings

Common properties for <shape> neutron nodes:

<gradient> Gradients

android:startcolor Start color

android:endcolor End Color

android:angle Gradient angle, 0 from top to bottom, 90 for left to right, the value of 45 integer times default to 0;

android:type gradient style liner linear gradient radial annular gradient sweep

<solid > Filling

Color of Android:color fills

<stroke > Stroke

Width of android:width Stroke

Color of android:color strokes

android:dashwidth means '-' the width of the horizontal line

android:dashgap says '-' the distance between horizontal lines

<corners > rounded Corners

The radius of the Android:radius fillet is larger, the corner becomes more rounded.

Android:toprightradius radius of upper right corner

Android:bottomleftradius angle radius of right corner

Android:topleftradius radius of upper left corner

Android:bottomrightradius radius of the lower left corner

2.Selector

Brief introduction

Location:res/drawable/The name of the file. xml

Methods to use:

in Java code: R.drawable. The name of the file

xml:android:background= "@drawable/File name"

<?xml version= "1.0" encoding= "Utf-8"?> <selector xmlns:android= 
"http://schemas.android.com/apk/res/" Android > 
<!--default background picture--> 
<item android:drawable= "@drawable/pic1"/> 
 
<!-- Background picture with no focus--> 
<item 
 android:state_window_focused= "false 
 android:drawable=" @drawable/pic_ Blue " 
 /> 
 
< background picture--> 
<item 
 android:state_focused=" true " 
 when the focus is obtained in non-touch mode and clicked Android:state_pressed= "true" 
 android:drawable= "@drawable/pic_red" 
 /> 
 
<!--the background picture when clicked in touch mode-- > 
<item 
 android:state_focused= "false" 
 android:state_pressed= "true" 
 android:drawable= The 
 
picture background--> 
<item 
 android:state_selected= "true" @drawable/pic_pink/> <!--selected 
 android:drawable= "@drawable/pic_orange" 
 /> 
 
<!--get focus when the picture background--> 
<item 
 Android:state_focused= "true" 
 android:drawable= "@drawable/pic_green" 
 /> 

Let's look at the structure of this example:

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:en Dcolor= "#99BD4C" android:startcolor= "#A5D245"/> <size android:height= "60DP" android:width= "320DP"/> &L T;corners android:radius= "8DP"/> </shape> </item> <item android:state_pressed= "true" > <sh ape> <gradient android:angle= "270" android:endcolor= "#99BD4C" android:startcolor= "#A5D245"/> <size an  
 droid:height= "60DP" android:width= "320DP"/> <corners android:radius= "8dp"/> </shape> </item> <item> <shape> <gradient android:angle= "270" android:endcolor= #A8C3B0 "android:startcolor=" #C6C FCE "/> <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>  
 

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.java

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[]{"", "UK", "USA", "Japan", "German", "Canada", "ET", "Narotu"}; /** called the activity is a. 
 * * @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 

Effect Chart:

The above is to the Android shape and selector data collation, have developed this piece of students can see.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.