侧滑抽屉布局-DrawerLayout的初体验.关于点击侧滑区域就关闭的问题.

来源:互联网 发布:讯猫软件 编辑:程序博客网 时间:2024/05/29 18:07

近来在做一个平板的项目.讲道理,平板的横布局,让我这经常写手机APP的有点不适应-.-;调试只能在真机,因为AS的布局预览里根本显示不全好嘛…

废话不多说,有个功能用到了侧滑效果,需要从布局左往右滑出.记得鸿神博客里提过有个组件叫DrawerLayout,于是在布局里就尝试了下.

总体来说体验还很好.

代码是最好的老师,上干货.

首先是XML文件

   <!-- 默认显示的布局界面 -->    <android.support.v4.widget.DrawerLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:id="@+id/drawer_layout"        >       <LinearLayout           android:layout_width="match_parent"           android:layout_height="match_parent"           android:gravity="center"           >           <LinearLayout               android:layout_width="325dp"               android:layout_height="wrap_content"               android:paddingLeft="10dp"               android:paddingTop="18dp"               android:paddingRight="13dp"               android:orientation="vertical"               android:paddingBottom="9dp"               android:background="@drawable/bg"               android:layout_centerVertical="true"               android:gravity="center"               android:layout_centerHorizontal="true">               <LinearLayout                   android:layout_width="match_parent"                   android:layout_marginBottom="10dp"                   android:layout_height="54dp">                   <TextView                       android:layout_width="83dp"                       android:layout_height="match_parent"                       android:text="@string/choose_product"                       android:textSize="17sp"                       android:gravity="center_vertical"                       android:textColor="#333333"                       />                   <TextView                       android:layout_width="match_parent"                       android:layout_height="match_parent"                       android:paddingRight="10dp"                       android:text="请选择"                       android:id="@+id/choose_product"                       android:textColor="@color/white"                       android:textSize="17sp"                       android:paddingLeft="13dp"                       android:gravity="center_vertical"                       android:background="@drawable/login_et_bg"                       android:drawableRight="@drawable/down"                       />               </LinearLayout>           </LinearLayout>       </LinearLayout>        <!-- 抽屉内容的布局 -->        <RelativeLayout            android:id="@+id/drawer_content"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_gravity="start"            android:background="@android:color/white" />    </android.support.v4.widget.DrawerLayout>

在这里要必须给大家提示下,有几个小坑:

1.抽屉内容的布局里的:

 android:layout_gravity="start"

是让抽屉效果从左滑入;相反 end 是从右往左滑入.

其他的没有试.大家可以体验下.

2.## 抽屉效果一定要在主布局的XML里Layout的下方.不知道为什么,Google官方API就是这么写的. ##
不然的话,你的抽屉弹出层,任何点击事件都会关闭抽屉.贼尴尬-.-
博主就踩到了这个坑…纠结了半天,所以,要看源码呀!~

别的就希望大家继续补充了.


代码首先没什么说的,创建对象,初始化.

  mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);  drawer_content = (RelativeLayout) findViewById(R.id.drawer_content);

其次,对你的抽屉布局里放入默认的fragment.

  //显示默认fragment        getSupportFragmentManager().beginTransaction().replace(R.id.drawer_content, new SelectFragment()).commit();

最后,在你需要滑出的时候,打开抽屉就可以啦~

   mDrawerLayout.openDrawer(drawer_content);//打开抽屉内容

接着在fragment里操作你的业务就可以拉~

然后是在fragment里要和你的activity里布局关联上.

这里的findViewById是要getActivity的哦~

 mDrawerLayout = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout); drawer_content = (RelativeLayout) getActivity().findViewById(R.id.drawer_content);

最后,在你不想看到它的时候就关了吧.

  mDrawerLayout.closeDrawer(drawer_content);

因为是项目里的代码,只能粘出来一点.不能给大家demo了.

但是也很详细,只要小心那2个坑,别的就没什么了.

欢迎大家补充,纠正~

最后,祝大家新年快乐.

0 0
原创粉丝点击