官方的Demo里有DrawerLayout的例子,涉及到ActionBar,这里不用ActionBar,手痒,写个超级简单的小Demo,备着以后或许会用到。
详细的内容,可以访问:http://blog.csdn.net/songzhiyong1121/article/details/8949329
看代码:
DrawerActivity.java
1 public class DrawerActivity extends Activity implements OnClickListener { 2 private DrawerLayout mDrawerLayout = null; 3 private Button bt1; 4 private Button bt2; 5 private Button bt3; 6 7 @Override 8 protected void onCreate(Bundle savedInstanceState) { 9 super.onCreate(savedInstanceState); 10 setContentView(R.layout.activity_drawer); 11 bt1 = (Button) findViewById(R.id.btn); 12 bt2 = (Button) findViewById(R.id.btn1); 13 bt3 = (Button) findViewById(R.id.btn2); 14 bt1.setOnClickListener(this); 15 bt2.setOnClickListener(this); 16 bt3.setOnClickListener(this); 17 mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 18 mDrawerLayout.setDrawerListener(new DrawerListener() { 19 20 @Override 21 public void onDrawerStateChanged(int arg0) { 22 Log.d("David", "onDrawerStateChanged arg0 = " + arg0); 23 } 24 25 @Override 26 public void onDrawerSlide(View arg0, float arg1) { 27 Log.d("David", "onDrawerSlide arg1 = " + arg1); 28 } 29 30 @Override 31 public void onDrawerOpened(View arg0) { 32 Log.d("David", "onDrawerOpened"); 33 } 34 35 @Override 36 public void onDrawerClosed(View arg0) { 37 Log.d("David", "onDrawerClosed"); 38 } 39 }); 40 } 41 42 @Override 43 public void onClick(View v) { 44 switch (v.getId()) { 45 case R.id.btn: 46 mDrawerLayout.openDrawer(Gravity.LEFT); 47 Toast.makeText(this, "bt1111111111", Toast.LENGTH_LONG).show(); 48 break; 49 case R.id.btn1: 50 Toast.makeText(this, "bt2222222222", Toast.LENGTH_LONG).show(); 51 break; 52 case R.id.btn2: 53 Toast.makeText(this, "bt33333333333", Toast.LENGTH_LONG).show(); 54 break; 55 default: 56 break; 57 } 58 } 59 60 }
activity_drawer.xml
1 <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:id="@+id/drawer_layout" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" > 5 6 <FrameLayout 7 android:id="@+id/content_frame" 8 android:layout_width="match_parent" 9 android:layout_height="match_parent" > 10 11 <Button 12 android:id="@+id/btn" 13 android:layout_width="match_parent" 14 android:layout_height="wrap_content" 15 android:text="open" /> 16 </FrameLayout> 17 18 <LinearLayout 19 android:id="@+id/left_drawer" 20 android:layout_width="240dp" 21 android:layout_height="match_parent" 22 android:layout_gravity="start" 23 android:orientation="vertical" 24 android:background="#eee" 25 > 26 27 <Button 28 android:id="@+id/btn1" 29 android:layout_width="match_parent" 30 android:layout_height="wrap_content" 31 android:text="open11" /> 32 33 <Button 34 android:id="@+id/btn2" 35 android:layout_width="match_parent" 36 android:layout_height="wrap_content" 37 android:text="open22" /> 38 </LinearLayout> 39 40 </android.support.v4.widget.DrawerLayout>