安卓侧滑之DrawerLayout
来源:互联网 发布:js实现点击图片放大 编辑:程序博客网 时间:2024/05/17 07:56
安卓原来用的侧滑方式是使用第三方,也就是使用SliddingMenu。这个已经有点古老了,在此不多做介绍。
DrawerLayout 是一个抽屉容器,来自support-v4包里面的。(android.support.v4.widget)。
使用上主要是在xml布局文件中,
简单使用:主要是侧滑部分android:layout_gravity="start"这个属性,start是左侧,end是右侧
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.hp.md.DrawerLayout_Activity"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" ></android.support.v7.widget.Toolbar> <android.support.v4.widget.DrawerLayout xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawerLayout" android:layout_below="@id/toolbar" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.hp.md.DrawerLayout_Activity"> <!--内容界面--> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是内容界面" /> </LinearLayout> <!-- 侧滑菜单左侧部分 --> <LinearLayout android:layout_width="200dp" android:layout_height="fill_parent" android:layout_gravity="start" android:background="#0f0" android:orientation="vertical" android:paddingTop="50dp"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ff0" android:text="item1" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ff0" android:text="item2" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ff0" android:text="item3" /> </LinearLayout> <!-- 侧滑菜单右侧部分 --> <LinearLayout android:layout_width="200dp" android:layout_height="fill_parent" android:layout_gravity="end" android:background="#0f0" android:orientation="vertical" android:paddingTop="50dp"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ff0" android:text="item1" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ff0" android:text="item2" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ff0" android:text="item3" /> </LinearLayout> </android.support.v4.widget.DrawerLayout></RelativeLayout>
简单优化一了下,引入Toolbar,然后在Activity里面设置同步监听
toolbar = (Toolbar) findViewById(R.id.toolbar);//将actionBar替换成toolbarsetSupportActionBar(toolbar);drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close);//同步状态actionBarDrawerToggle.syncState();//给侧滑控件设置监听drawerLayout.setDrawerListener(actionBarDrawerToggle);
--------------------------------------------------------------------------------------------------------简易版已经完成------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------DIY版本---------------------------------------------------------------------------------------------------------------
//给侧滑控件设置监听
drawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() { @Override public void onDrawerStateChanged(int newState) { // 状态发生改变// switch (newState) {// case DrawerLayout.STATE_DRAGGING:// Log.i(TAG, "拖动状态");// break;// case DrawerLayout.STATE_IDLE:// Log.i(TAG, "静止状态");// break;// case DrawerLayout.STATE_SETTLING:// Log.i(TAG, "设置状态");// break;// default:// break;// } } @Override public void onDrawerSlide(View drawerView, float slideOffset) { // 滑动的过程当中不断地回调 slideOffset:0~1 View content = drawerLayout.getChildAt(0); View menu = drawerView; float scale = 1 - slideOffset;//1~0 float leftScale = (float) (1 - 0.3 * scale); float rightScale = (float) (0.7f + 0.3 * scale);//0.7~1 menu.setScaleX(leftScale);//1~0.7 menu.setScaleY(leftScale);//1~0.7 content.setScaleX(rightScale); content.setScaleY(rightScale); content.setTranslationX(menu.getMeasuredWidth() * (1 - scale));//0~width } @Override public void onDrawerOpened(View drawerView) { // 打开 } @Override public void onDrawerClosed(View drawerView) { // 关闭 } });
0 0
- 安卓侧滑之DrawerLayout
- 安卓侧滑之DrawerLayout+NavigationView
- DrawerLayout 之 抽屉菜单
- Android UI之DrawerLayout
- Android开发之DrawerLayout
- android组件之DrawerLayout
- Android学习之DrawerLayout
- 安卓侧滑菜单搭建-drawerlayout
- 侧滑菜单之 DrawerLayout
- DrawerLayout与ToolBar之双宿双飞
- NavigationView与Drawerlayout之双宿双飞
- Material Design控件之DrawerLayout
- DrawerLayout
- DrawerLayout
- DrawerLayout
- DrawerLayout
- DrawerLayout
- DrawerLayout
- Solr搜索引擎开发初试(2)(接"Lucene搜索引擎开发初试(1)")
- android 如何判断当前是否为飞行模式
- 恩布开源安卓手机IM,EntboostIM全新发布1.6.0版本
- 使用ls筛选某一天的文件
- Bootstrap的datatable控件
- 安卓侧滑之DrawerLayout
- vs环境下QT新建.ui文件无法自动生成ui_xxx.h
- SQL 大数据查询如何进行优化?
- 网络通信协议
- No rule to make target cocos2d/cocos/editor-support/cocostudio/CCActionFrame.cpp
- 计算机网络概述(二)
- 用位操作改变内存中的值
- 根据jks文件获取SHA1签名
- UGUI修改Text组件字间距代码