码迷,mamicode.com
首页 > 移动开发 > 详细

【安卓笔记】抽屉式布局----DrawerLayout

时间:2017-05-07 17:42:24      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:set   ica   water   div   tran   cal   instance   save   java   

效果例如以下:

技术分享技术分享

DrawerLayout来自support.v4包,所以不用考虑兼容性问题。其次,这样的布局类似风靡一时的側滑菜单,可是比側滑菜单轻巧很多。
以下介绍这样的布局的使用方式。
1.在你的项目中导入support.v4包。
2.编辑一个布局,根节点为android.support.v4.widget.DrawerLayout,此节点下仅仅同意有两个子节点,第一个为将来主页面的内容,第二个节点即为"抽屉"内容,一般是一个ListView。比方:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <LinearLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈"
            android:textColor="#000"
            android:textSize="20sp" />
        <Button
            android:id="@+id/but_open"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="开启抽屉" />
    </LinearLayout>
    <!-- 一定要加 layout_gravity="start" -->
    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#111"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
3.编写代码,监听相关事件:
package com.example.drawerlayoutdemo1;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends Activity
{
	protected static final String TAG = "MainActivity";
	private DrawerLayout mDrawerLayout = null;
	private ListView lv = null;
	private Button but_open = null;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		lv = (ListView) findViewById(R.id.left_drawer);
		but_open = (Button) findViewById(R.id.but_open);
		but_open.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				mDrawerLayout.openDrawer(Gravity.LEFT);
			}
		});
		lv.setAdapter(new ArrayAdapter<String>(this,R.layout.test,R.id.tv_item,new String[]{"条目1","条目2","条目3"}));
		mDrawerLayout.setDrawerListener(new DrawerLayout.SimpleDrawerListener()
		{
			@Override
			public void onDrawerClosed(View drawerView)
			{
				Log.i(TAG,"抽屉关闭了...");
			}
			@Override
			public void onDrawerOpened(View drawerView)
			{
				Log.i(TAG,"抽屉打开了...");
			}
			@Override
			public void onDrawerSlide(View drawerView, float slideOffset)
			{
				Log.i(TAG,"抽屉在滑动...");
			}
			@Override
			public void onDrawerStateChanged(int newState)
			{
				switch (newState)
				{
				case DrawerLayout.STATE_DRAGGING:
					Log.i(TAG,"拖动状态");
					break;
				case DrawerLayout.STATE_IDLE:
					Log.i(TAG,"精巧状态");
					break;
				case DrawerLayout.STATE_SETTLING:
					Log.i(TAG,"设置状态");
					break;
				default:
					break;
				}
			}
		});
		lv.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?

> parent, View view, int position, long id) { switch (position) { case 0: Log.i(TAG,"条目1被点击"); break; case 1: Log.i(TAG,"条目2被点击"); break; case 2: Log.i(TAG,"条目3被点击"); break; } } }); } }











【安卓笔记】抽屉式布局----DrawerLayout

标签:set   ica   water   div   tran   cal   instance   save   java   

原文地址:http://www.cnblogs.com/gccbuaa/p/6821280.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!