美文网首页
Drawerlayout相关使用方法

Drawerlayout相关使用方法

作者: 指云商动 | 来源:发表于2018-03-27 11:12 被阅读0次

Drawerlayout是support.v4下的类,所以在使用时需要引入。接下来简单说一下使用:

1、在xml布局文件中需要将主界面布局和侧滑布局都包含在drawerlayout里面,左面的侧滑布局设置layout_gravity=start或者left,右面的侧滑布局设置为end或者right,需要注意的是主布局需要放置在最上面,同时如果侧滑布局里面的控件想发生点击事件,需要在侧滑布局的根布局添加clickable=true属性。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mDrawerlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

 <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/mid"
        android:background="@android:color/white" />

    <LinearLayout
        android:id="@+id/left"
        android:layout_gravity="left"
        android:clickable="true"
        android:layout_width="200dp"
        android:layout_height="match_parent" >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/v4_text"
            android:textSize="22sp"
            android:textColor="#00ff00"
            android:gravity="center"
            />
    </LinearLayout>

</android.support.v4.widget.DrawerLayout>

2、接下来就可以在java代码中进行监听控制了,比如说要在主界面上点击头像来自动滑动侧滑菜单。

touxiang_img.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mDrawerLayout.openDrawer(Gravity.START);
//对应的当然有 mDrawerLayout.closeDrawer(Gravity.LEFT); 来关闭
            }
        });

相关文章

网友评论

      本文标题:Drawerlayout相关使用方法

      本文链接:https://www.haomeiwen.com/subject/xgpjcftx.html