在做项目时,使用到了左滑出现菜单栏,遵循了原来学习的使用SlidingMenu开源库,但是对于有的手机居然报了错(#80 java.lang.StackOverflowError
com.jeremyfeinstein.slidingmenu.lib.CustomViewAbove.dispatchDraw(CustomViewAbove.java:827)),于是考虑使用drawlayout这个控件取代它,于是对这个控件研究了一番,现在写出基本使用步骤,以便日后回顾。这里只是介绍最基本的用法。
步骤
- 准备左右两边和内容区域的菜单布局
- 将布局加载进Fragment里面
- 在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();
}