如何实现两级 DrawerLayout

 我来答
匿名用户
2016-02-09
展开全部
1主页布局文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="hschemas.android.com/apk/res/android"
xmlns:tools="schemas.android.com/tools"
android:id="@+id/main_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent" >

<RelativeLayout
android:id="@+id/main_content_frame_parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent" >
<!-- 下层显示的主要内容 -->
<FrameLayout
android:id="@+id/main_content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:scrollbars="vertical" >
</FrameLayout>
</RelativeLayout>
<!-- 左侧滑动栏 -->
<RelativeLayout
android:id="@+id/main_left_drawer_layout"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/transparent"
android:paddingTop="50dp" >
</RelativeLayout>
<!-- 右侧滑动栏 -->

<RelativeLayout
android:id="@+id/main_right_drawer_layout"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="@android:color/transparent"
android:paddingTop="50dp" >
</RelativeLayout>

</android.support.v4.widget.DrawerLayout>
如上,使用DrawerLayout需要在布局文件跟目录中引用,v4包中的DrawerLayout标签,并且宽和高,都设置为match_parent.里面framelayout用来现实菜单收起时,下层页面的布局。
而main_left_drawer_layout和main_right_drawer_layout为左右两个抽屉菜单对应的父layout,需要注意的是,在DrawerLayout中,从左侧开始使用android:layout_gravity="start",从右侧开始,使用 android:layout_gravity="end"。
b.主布局代码文件:

package com.demo.drawlayout;

import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainFrameLayout extends FragmentActivity {
// 抽屉菜单对象
private ActionBarDrawerToggle drawerbar;
public DrawerLayout drawerLayout;
private TestFragment testFragment;
private RelativeLayout left_menu_layout, right_xiaoxi_layout;

private TextView text;
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.main_frame_activity);
initView();
initEvent();
}
public void initView(){
testFragment = new TestFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction f_transaction = fragmentManager.beginTransaction();
f_transaction.replace(R.id.main_content_frame_parent, testFragment);
f_transaction.commitAllowingStateLoss();
initLeftLayout();
initRightLayout();
}
public void initLeftLayout(){
drawerLayout = (DrawerLayout) findViewById(R.id.main_drawer_layout);
//设置透明
drawerLayout.setScrimColor(0x00000000);
//左边菜单
left_menu_layout = (RelativeLayout) findViewById(R.id.main_left_drawer_layout);
View view2 = getLayoutInflater().inflate(R.layout.menu_layout, null);
text=(TextView)view2.findViewById(R.id.text);
text.setText("左边测试菜单");
left_menu_layout.addView(view2);
}
public void initRightLayout(){
//左边菜单
right_xiaoxi_layout = (RelativeLayout) findViewById(R.id.main_right_drawer_layout);
View view = getLayoutInflater().inflate(R.layout.xiaoxi_layout, null);
text=(TextView)view.findViewById(R.id.text);
text.setText("右边测试菜单");
right_xiaoxi_layout.addView(view);
}
private void initEvent() {
drawerbar = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_launcher, R.string.open, R.string.close) {
//菜单打开
@Override
public void onDrawerOpened(View drawerView) {

super.onDrawerOpened(drawerView);
}
// 菜单关闭
@Override
public void onDrawerClosed(View drawerView) {

super.onDrawerClosed(drawerView);
}
};
drawerLayout.setDrawerListener(drawerbar);
}
//左边菜单开关事件
public void openLeftLayout() {
if (drawerLayout.isDrawerOpen(left_menu_layout)) {
drawerLayout.closeDrawer(left_menu_layout);
} else {
drawerLayout.openDrawer(left_menu_layout);

}
}

// 右边菜单开关事件
public void openRightLayout() {
if (drawerLayout.isDrawerOpen(right_xiaoxi_layout)) {
drawerLayout.closeDrawer(right_xiaoxi_layout);
} else {
drawerLayout.openDrawer(right_xiaoxi_layout);
}
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式