您的位置:首页 > 其它

DrawerLayout 抽屉菜单效果使用实例介绍

2016-03-02 11:18 288 查看
在我们使用安卓的很多app的时候,常常能看到类似抽屉开关样式的布局界面风格,它的实现方式也很简单,通过在xml文件中定义一些相关属性,便可以实现类似的效果。
 
DrawerLayout 抽屉菜单效果   完成效果 : 左侧滑菜单 点击菜单中不同item  主界面切换 ,右侧滑文字展示 ;

 
1、在对应的Activity 中,使用v4包android.support.v4.weiget.DrawerLayout
在其中设置子布局,将布局的gravity 属性设置成start (左划侧边栏),center(初始界面),end(右划侧边栏)会显示DrawerLayout 抽屉效果。
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity">

<android support="" v4="" widget="" drawerlayout="" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white">

<framelayout android:id="@+id/fragment_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white">

<textview android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="XDL兄弟连IT培训基地,现在正在火热报名招生中">

<android support="" v4="" view="" viewpager="" android:id="@+id/act_main_pager" android:layout_width="fill_parent" android:layout_height="fill_parent">
</android></textview></framelayout>

<linearlayout android:id="@+id/menu_layout" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="@android:color/white" android:orientation="vertical">

<imageview android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:src="@drawable/ic_launcher">

<textview android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="侧拉菜单" android:textcolor="@android:color/black">

<listview android:id="@+id/menu_listView" android:layout_width="match_parent" android:layout_height="match_parent" android:cachecolorhint="#FFFFFF">
</listview>
</textview></imageview></linearlayout>

87e1
<framelayout android:id="@+id/sidebar_container" android:layout_width="250dp" android:layout_height="match_parent" android:layout_gravity="end" android:background="#FFFFFF">

<!-- 在这个FrameLayout之间 写一个ListView -->

<listview android:id="@+id/act_list" android:layout_width="wrap_content" android:layout_height="wrap_content">
</listview>
</framelayout>

</android></linearlayout>



在任意一个布局中,加上Id,使用Viewpager,进行点击后,界面跳转的显示需要先绑定上fragment

lv.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// 列表中内容的点击事件处理!
//对应的点击item的坐标对应fragment的坐标
mViewPager.setCurrentItem(arg2);
}
});


好了,到这里抽屉效果的展示就可以实现了,如有什么不懂的问题,可以联系本人一起交流哦~

想了解更多安卓知识吗?-----请联系:兄弟连王老师
QQ :1981812120
TEL:13011652089
悄悄告诉你们,兄弟连安卓学科现在优惠大大滴~还在为自学安卓苦恼吗?来加入兄弟连,更多请详情:兄弟连王老师
微信:13011652089
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: