1.首先定义主界面的布局文件activity_main.xml
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 tools:context="com.ll.testdrawerlayout.MainActivity" > 6 7 <android.support.v4.widget.DrawerLayout 8 android:id="@+id/drawer_layout" 9 android:layout_width="match_parent" 10 android:layout_height="match_parent" > 11 12 <!-- The main content view --> 13 14 <FrameLayout 15 android:id="@+id/content_frame" 16 android:layout_width="match_parent" 17 android:layout_height="match_parent" > 18 19 <Button 20 android:id="@+id/btn" 21 android:layout_width="match_parent" 22 android:layout_height="wrap_content" 23 android:text="open" 24 /> 25 </FrameLayout> 26 27 <!-- The navigation drawer --> 28 29 <ListView 30 android:id="@+id/left_drawer" 31 android:layout_width="240dp" 32 android:layout_height="match_parent" 33 android:layout_gravity="start" 34 android:background="#111" 35 android:choiceMode="singleChoice" 36 android:divider="@android:color/transparent" 37 android:dividerHeight="0dp" /> 38 </android.support.v4.widget.DrawerLayout> 39 40 </RelativeLayout>
2.定义列表项的布局list_item.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:background="#fff0ff" 6 android:orientation="horizontal" > 7 8 <ImageView 9 android:id="@+id/img" 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:layout_margin="30dp" 13 android:scaleType="centerCrop"/> 14 <TextView 15 android:id="@+id/info" 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content" 18 android:layout_gravity="center_vertical" 19 android:textColor="#000000" 20 android:textSize="22sp" /> 21 22 </LinearLayout>
3.在主程序中初始化列表和添加列表的消息响应函数
1 package com.ll.testdrawerlayout; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7 8 9 import android.support.v4.widget.DrawerLayout; 10 import android.support.v7.app.ActionBarActivity; 11 import android.os.Bundle; 12 import android.view.Gravity; 13 import android.view.Menu; 14 import android.view.MenuItem; 15 import android.view.View; 16 import android.view.View.OnClickListener; 17 import android.widget.AdapterView; 18 import android.widget.AdapterView.OnItemClickListener; 19 import android.widget.Button; 20 import android.widget.ListView; 21 import android.widget.SimpleAdapter; 22 import android.widget.Toast; 23 24 25 public class MainActivity extends ActionBarActivity { 26 27 private DrawerLayout mDrawerLayout = null; 28 Object[] icArray = new Object[8]; 29 String[] itemTitle; 30 SimpleAdapter simpleAdapter = null; 31 ListView listView = null; 32 33 @Override 34 protected void onCreate(Bundle savedInstanceState) { 35 super.onCreate(savedInstanceState); 36 setContentView(R.layout.activity_main); 37 38 mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout); 39 40 Button button = (Button) findViewById(R.id.btn); 41 button.setOnClickListener(new OnClickListener() 42 { 43 44 @Override 45 public void onClick(View v) 46 { 47 // 按钮按下,将抽屉打开 48 mDrawerLayout.openDrawer(Gravity.LEFT); 49 50 } 51 }); 52 53 listView = (ListView)findViewById(R.id.left_drawer); 54 getIC(); 55 itemTitle = getResources().getStringArray(R.array.item_title); 56 simpleAdapter = new SimpleAdapter(this,getData(),R.layout.list_item, new String[]{"info","img"}, 57 new int[]{R.id.info, R.id.img}); 58 listView.setAdapter(simpleAdapter); 59 // 为列表添加消息响应函数 60 listView.setOnItemClickListener(new ItemClickListener()); 61 } 62 63 64 65 @Override 66 public boolean onCreateOptionsMenu(Menu menu) { 67 // Inflate the menu; this adds items to the action bar if it is present. 68 getMenuInflater().inflate(R.menu.main, menu); 69 return true; 70 } 71 72 @Override 73 public boolean onOptionsItemSelected(MenuItem item) { 74 // Handle action bar item clicks here. The action bar will 75 // automatically handle clicks on the Home/Up button, so long 76 // as you specify a parent activity in AndroidManifest.xml. 77 int id = item.getItemId(); 78 if (id == R.id.action_settings) { 79 return true; 80 } 81 return super.onOptionsItemSelected(item); 82 } 83 84 private List<Map<String, Object>> getData(){ 85 List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); 86 87 for(int i = 0; i < icArray.length; i ++) { 88 Map<String, Object> map = new HashMap<String, Object>(); 89 map.put("img", icArray[i]); 90 map.put("info", itemTitle[i]); 91 list.add(map); 92 } 93 return list; 94 } 95 96 private void getIC(){ 97 icArray[0] = R.drawable.ic_drawer_home_normal; 98 icArray[1] = R.drawable.ic_drawer_explore_normal; 99 icArray[2] = R.drawable.ic_drawer_follow_normal; 100 icArray[3] = R.drawable.ic_drawer_collect_normal; 101 icArray[4] = R.drawable.ic_drawer_draft_normal; 102 icArray[5] = R.drawable.ic_drawer_search_normal; 103 icArray[6] = R.drawable.ic_drawer_question_normal; 104 icArray[7] = R.drawable.ic_drawer_setting_normal; 105 } 106 107 class ItemClickListener implements OnItemClickListener{ 108 109 @Override 110 public void onItemClick(AdapterView<?> parent, View view, int position, 111 long id) { 112 // TODO Auto-generated method stub 113 Map<String, Object> item = (Map<String, Object>)parent.getItemAtPosition(position); 114 Toast.makeText(getApplicationContext(), (String)item.get("info"), Toast.LENGTH_LONG).show(); 115 } 116 117 }// end of ItemClickListener class 118 }
其实未来的路还很长,不管当初是什么原因让我选择了这条路,我一定会坚持下去的。
既然选择了远方,不管前面的艰难险阻,只顾风雨兼程。
原文:http://www.cnblogs.com/liul/p/4941328.html