Android 控件DrawerLayout的基本使用

在做项目时,使用到了左滑出现菜单栏,遵循了原来学习的使用SlidingMenu开源库,但是对于有的手机居然报了错(#80 java.lang.StackOverflowError

com.jeremyfeinstein.slidingmenu.lib.CustomViewAbove.dispatchDraw(CustomViewAbove.java:827)),于是考虑使用drawlayout这个控件取代它,于是对这个控件研究了一番,现在写出基本使用步骤,以便日后回顾。这里只是介绍最基本的用法。

步骤

  1. 准备左右两边和内容区域的菜单布局
  2. 将布局加载进Fragment里面
  3. 在Activity的布局文件里面添加进左右菜单区域

左边布局文件(fg_left.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#00ff00"
android:layout_height="match_parent">

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    android:gravity="center"
    android:text="左边菜单" />
</LinearLayout>

左边的fragment Java代码,主要是将布局加载进来

public class LeftFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fg_left, container, false);
    return view;
}
}

右边的布局文件(fg_right.xml)

`<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff100"
android:gravity="center"
android:orientation="vertical">

<TextView
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="右边菜单" />

</LinearLayout>`

右边的fragment Java代码,主要是将布局加载进来

public class RightFragment extends Fragment {


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fg_right, container, false);
    return view;
}
}

内容区域fragment的布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
    android:text="我是内容区域,可以左右滑动吆"
    android:id="@+id/tv_content"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:textSize="25sp" />

</RelativeLayout>

内容区域的fragment的Java代码

public class ContentFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fg_content, container, false);
    return view;
}
}

activity的布局文件,注意:左右两个抽屉的布局文件一定要是DrawerLayout的直接儿子。

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">


<FrameLayout
    android:id="@+id/fly_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />


<fragment
    android:id="@+id/fg_left_menu"
    android:name="www.dshui.cc.testdrawlayout.LeftFragment"
    android:layout_width="300dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:tag="LEFT"
    tools:layout="@layout/fg_left" />

<fragment
    android:id="@+id/fg_right_menu"
    android:name="www.dshui.cc.testdrawlayout.RightFragment"
    android:layout_width="300dp"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    android:tag="RIGHT"
    tools:layout="@layout/fg_right" />

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

在启动Activity时直接替换掉Activity布局中的framlayout.

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    getSupportFragmentManager().beginTransaction().replace(R.id.fly_content,new ContentFragment()).commit();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值