Sometimes we want to display another layout on one page, but we do not need to occupy too much layout space. In this case, we can consider using drawer layout. This term can be used to make full use of our imagination, I believe that you will be able to understand this method through the introduction below.
Now, let's go directly to a simple small project.
1. First, create a main activity.
Package COM. jindegege. activity; import COM. jindegege. service. myadapter; import android. app. activity; import android. OS. bundle; import android. widget. gridview; import android. widget. imageview; import android. widget. slidingdrawer; public class slidingdraweractivity extends activity {private gridview; private slidingdrawer; private imageview; private int [] icons = {R. drawable. main1, R. drawable. main2, R. drawable. main3, R. drawable. main4, R. drawable. main5, R. drawable. main6, R. drawable. main7, R. drawable. main8, R. drawable. main9}; private string [] items = {"", "", "jindegege", "Lao Mao ", "Old bi", "Han"};/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); gridview = (gridview) findviewbyid (R. id. gridview); slidingdrawer = (slidingdrawer) findviewbyid (R. id. SD); imageview = (imageview) findviewbyid (R. id. imageview); myadapter adapter = new myadapter (this, items, icons); // instantiate a myadapter object through the constructor. This myadapter object must inherit the baseadapter class gridview. setadapter (adapter); slidingdrawer. setondraweropenlistener (New slidingdrawer. ondraweropenlistener () // open the drawer {@ override public void ondraweropened () {imageview. setimageresource (R. drawable. photo); // open drawer event}); slidingdrawer. setondrawercloselistener (New s0000ingdrawer. ondrawercloselistener () {@ override public void ondrawerclosed () {imageview. setimageresource (R. drawable. ic_launcher); // close drawer event }});}}
2. Create the layout file to be loaded for this main activity.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:textSize="20sp" /> <SlidingDrawer android:id="@+id/sd" android:layout_width="fill_parent" android:layout_height="fill_parent" android:handle="@+id/imageview" android:content="@+id/gridview" android:orientation="vertical" > <ImageView android:id="@id/imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <GridView android:id="@id/gridview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numColumns="3" android:background="#EE82EE" android:gravity="center" /> </SlidingDrawer></RelativeLayout>
Step 3: In the main activity, we need to instantiate a myadapter object through the constructor. This myadapter object must inherit the baseadapter class,
To use a custom adapter, first create an XML file item. XML, which is just a simple style.
<?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="fill_parent"> <ImageView android:id="@+id/icon" android:layout_width="wrap_content" android:layout_height="40px" android:layout_gravity="center" /> <TextView android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:textColor="#000000" /></LinearLayout>
Step 4: design the custom class at this time.
Package COM. jindegege. service; import COM. jindegege. activity. r; import android. content. context; import android. view. layoutinflater; import android. view. view; import android. view. viewgroup; import android. widget. baseadapter; import android. widget. imageview; import android. widget. textview; public class myadapter extends baseadapter {private context; private string [] items; private int [] icons; Public myadapter (context, string [] items, int [] icons) // constructor {This. context = context; this. items = items; this. icons = icons;} @ override public int getcount () {return items. length ;}@ override public object getitem (INT arg0) {return items [arg0] ;}@ override public long getitemid (INT position) {return position ;} @ override public view getview (INT position, view convertview, viewgroup parent) {layoutinflater factory = layoutinflater. from (context); view v = (View) factory. inflate (R. layout. item, null); // bind custom Layout imageview IV = (imageview) v. findviewbyid (R. id. icon); textview TV = (textview) v. findviewbyid (R. id. text); IV. setimageresource (icons [position]); TV. settext (items [position]); Return v ;}}
Okay, I won't post it to you. You can download this small project and check the effect. You can also modify it to achieve better results.
Source code: http://download.csdn.net/detail/jindegegesun/4086564