抽屉效果的实现我们用到的控件有DrawerLayout和SlidingDrawer。当然网上还有好多大神们已经做好并且功能强大的抽屉框架了。能力有限只是会用大神们写好的框架但不怎么好改,所以就自己整理了下毕竟自己写的以后要改的话还能够知道怎么改。
1、DrawerLayout是支持包中的类所以它具有很好的兼容性,当然是用的时候也是需要进行具体指定的。那么在xml配置文件中是如下:
<!- 主标题 ->
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@color/green" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:background="@drawable/bar_assortment" />
<TextView
android:id="@+id/tv_title"
style="@style/textColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="标题" />
</RelativeLayout>
<!- 抽屉 ->
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/relativeLayout1" >
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white" >
</FrameLayout>
<FrameLayout
android:id="@+id/menu_frame"
android:layout_width="150dp"
android:layout_height="match_parent"
android:layout_gravity="left">
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
FrameLayout大家应该不会陌生吧,碎片。好处就是可管理性具体的大家可以找找相关方面的资料这就不多说了。抽屉主要分为两部分:第一部分就是“内容”也就是第一个FrameLayout,第二个就是“侧滑栏”也就是第二个FrameLayout。当然也可以直接把这两块的布局写上,我这里主要是为了管理代码的方便上。它们的属性应该没有什么主要是侧滑栏中的android:layout_gravity=”left”属性表示侧滑栏在左边,right表示在右边。
2、在代码上的处理比较简单:初始化一下各个控件。
a、如果希望侧滑栏滑出来的时候内容界面的背景不变暗的是需要设置一下:
drawerLayout.setScrimColor(Color.TRANSPARENT);
b、为了能够监测到侧滑栏的滑出状态所以需要初始化一下侧滑栏的FrameLayout,再通过:
drawerLayout.isDrawerOpen(frameLayout)判断侧滑栏是否已经滑出;
c、通过drawerLayout.closeDrawer(frameLayout);和drawerLayout.openDrawer(frameLayout);可以来操作侧滑栏的滑出和滑入;
3、内容和侧滑栏的处理是添加两个Fragment:
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ContentFragment content_frame = new ContentFragment();
Bundle bundle = new Bundle();
bundle.putString("title", "0");//这是为了传递标题的内容相应的fragment来改变标题
content_frame.setArguments(bundle);
ft.replace(R.id.content_frame, content_frame);
ft.replace(R.id.menu_frame, new MenuFragment());
ft.commit();
4、内容fragment:
public class ContentFragment extends PublicFragment{
@Override
public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_content, null);
setTitleText(getActivity(), R.id.tv_title, getArguments().getString("title"));
return view;
}
}
PublicFragment主要就是设置下标题获取标题控件然后setText(”“)设置标题内容;
5、侧滑栏fragment,我这里用的是ListView主要是条目点击事件的处理如下:
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ContentFragment content_frame = new ContentFragment();
Bundle bundle = new Bundle();
switch (position) {
case 0:
bundle.putString("title", "0");
break;
case 1:
bundle.putString("title", "1");
break;
case 2:
bundle.putString("title", "2");
break;
case 3:
bundle.putString("title", "3");
break;
}
onArticleSelectedListener.onArticleSelected();
content_frame.setArguments(bundle);
ft.replace(R.id.content_frame, content_frame);
ft.commit();
这里我用到了一个回调,主要是为了在点击条目的时候能够让侧滑栏滑回来。
好了,DrawerLayout 一个简简单单的抽屉就整好了,本人菜鸟写的不好勿喷…………………………..。
代码实例:http://download.youkuaiyun.com/detail/lihui_92_11_11/8612873