使用DrawerLayout实现侧拉菜单

侧拉菜单在android应用中非常常见,它的实现方式太多了,今天我们就说说使用Google提供的DrawerLayout来实现侧拉菜单效果,先来看张效果图:

DrawerLayout的实现其实非常简单,只要按照既有的规范来写即可,先来看看布局文件:

<android.support.v4.widget.DrawerLayout xmlns:android=""xmlns:tools=""android:id="@+id/drawerlayout"android:layout_width="match_parent"android:layout_height="match_parent" ><RelativeLayoutandroid:id="@+id/fragment_layout"android:layout_width="match_parent"android:layout_height="match_parent" ><RelativeLayoutandroid:id="@+id/title_bar"android:layout_width="match_parent"android:layout_height="48dp"android:background="#63B8FF" ><ImageViewandroid:id="@+id/leftmenu"android:layout_width="48dp"android:layout_height="48dp"android:padding="12dp"android:src="@drawable/menu" /><TextViewandroid:layout_width="wrap_content"android:layout_height="48dp"android:layout_centerInParent="true"android:gravity="center"android:text="Title Bar" /><ImageViewandroid:id="@+id/rightmenu"android:layout_width="48dp"android:layout_height="48dp"android:layout_alignParentRight="true"android:padding="12dp"android:src="@drawable/p_center" /></RelativeLayout><LinearLayoutandroid:id="@+id/content"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_below="@id/title_bar"android:orientation="vertical" /></RelativeLayout><RelativeLayoutandroid:id="@+id/left"android:layout_width="200dp"android:layout_height="match_parent"android:layout_gravity="left"android:background="@android:color/white" ><ListViewandroid:id="@+id/left_listview"android:layout_width="match_parent"android:layout_height="match_parent" ></ListView></RelativeLayout><RelativeLayoutandroid:id="@+id/right"android:layout_width="260dp"android:layout_height="match_parent"android:layout_gravity="right"android:background="#BCEE68" ><ImageViewandroid:id="@+id/p_pic"android:layout_width="72dp"android:layout_height="72dp"android:layout_centerInParent="true"android:src="@drawable/p_pic" /><TextViewandroid:id="@+id/right_textview"android:layout_width="wrap_content"android:layout_height="48dp"android:layout_below="@id/p_pic"android:layout_centerHorizontal="true"android:layout_marginTop="12dp"android:text="个人中心"android:textColor="@android:color/black"android:textSize="14sp" /></RelativeLayout></android.support.v4.widget.DrawerLayout>首先,根布局就是DrawerLayout,在根布局之后又主要分为三大块,第一块就是我们主界面的内容,第二块是左边拉出来的布局,第三块是右边拉出来的布局(不需要右边侧拉就不用写,这样的话整个布局就只分为两大块),那么系统怎么知道我们这个布局是主布局还是侧拉菜单的布局?请注意左边侧拉菜单布局android:layout_gravity="left"这个属性和右边菜单布局的android:layout_gravity="right"这个属性,哈哈,这下应该明白了吧,系统通过layout_gravity属性的值来判断这个布局是左边菜单的布局还是右边菜单的布局,如果没有这个属性,那不用说,肯定是主界面的布局。

好了,布局文件写好之后,我们的侧拉效果其实就已经可以实现了,但是你发现左边拉出来什么东西都没有,那是因为还没有数据,所以我们要在MainActivity中初始化左边布局的ListView,给ListView赋值这个想必大家都会,我就直接贴代码了:

listView = (ListView) findViewById(R.id.left_listview);initData();adapter = new ContentAdapter(this, list);listView.setAdapter(adapter);初始化数据的方法:

private void initData() {list = new ArrayList<ContentModel>();list.add(new ContentModel(R.drawable.doctoradvice2, "新闻", 1));list.add(new ContentModel(R.drawable.infusion_selected, "订阅", 2));list.add(new ContentModel(R.drawable.mypatient_selected, "图片", 3));list.add(new ContentModel(R.drawable.mywork_selected, "视频", 4));list.add(new ContentModel(R.drawable.nursingcareplan2, "跟帖", 5));list.add(new ContentModel(R.drawable.personal_selected, "投票", 6));}ContentModel类:

public class ContentModel {private int imageView;private String text;private int id;public int getId() {return id;}public void setId(int id) {this.id = id;}public ContentModel() {}public ContentModel(int imageView, String text, int id) {this.imageView = imageView;this.text = text;this.id = id;}public int getImageView() {return imageView;}public void setImageView(int imageView) {this.imageView = imageView;}public String getText() {return text;}public void setText(String text) {this.text = text;}}数据适配器:

世上没有绝望的处境,只有对处境绝望的人。

使用DrawerLayout实现侧拉菜单

相关文章:

你感兴趣的文章:

标签云: