1.首先来欣赏下效果图
2.先看看activity_main的布局,DrawerLayout里第一个布局时主内容,第二个布局是左侧,第三个布局是右侧。
17 8 12 13 21 2220 29 30 37 38
3.layout_menu是左侧布局
1 26 7 132 133 13412 13 16 17 36 3725 26 35 40 41 60 6149 50 59 64 65 84 8573 74 83 88 89 108 10997 98 107 112 113 121 122 131
1 package com.watson.lv.drawerlayoutdemo; 2 3 import android.os.Bundle; 4 import android.support.v4.app.Fragment; 5 import android.view.LayoutInflater; 6 import android.view.View; 7 import android.view.ViewGroup; 8 9 /**10 * Created by dell on 2018/3/28.11 */12 13 public class MenuLeftFragment extends Fragment14 {15 16 @Override17 public View onCreateView(LayoutInflater inflater, ViewGroup container,18 Bundle savedInstanceState)19 {20 return inflater.inflate(R.layout.layout_menu, container, false);21 }22 }
4.menu_layout_right是右侧布局。
1 27 8 51 5215 16 29 3021 22 28 37 38 43 44 50 59 60 65 66 72 73 74 81 82 87 88 94 95 96
1 package com.watson.lv.drawerlayoutdemo; 2 3 import android.os.Bundle; 4 import android.support.v4.app.Fragment; 5 import android.view.LayoutInflater; 6 import android.view.View; 7 import android.view.ViewGroup; 8 9 /**10 * Created by dell on 2018/3/28.11 */12 13 public class MenuRightFragment extends Fragment14 {15 16 @Override17 public View onCreateView(LayoutInflater inflater, ViewGroup container,18 Bundle savedInstanceState)19 {20 return inflater.inflate(R.layout.menu_layout_right, container, false);21 }22 }
5.最后展示出MainActivity
1 package com.watson.lv.drawerlayoutdemo; 2 3 import android.os.Bundle; 4 import android.support.v4.app.FragmentActivity; 5 import android.support.v4.widget.DrawerLayout; 6 import android.support.v4.widget.DrawerLayout.DrawerListener; 7 import android.view.Gravity; 8 import android.view.View; 9 import android.view.Window; 10 11 import com.nineoldandroids.view.ViewHelper; 12 13 public class MainActivity extends FragmentActivity 14 { 15 16 private DrawerLayout mDrawerLayout; 17 18 @Override 19 protected void onCreate(Bundle savedInstanceState) 20 { 21 super.onCreate(savedInstanceState); 22 requestWindowFeature(Window.FEATURE_NO_TITLE); 23 setContentView(R.layout.activity_main); 24 25 initView(); 26 initEvents(); 27 28 } 29 30 public void OpenRightMenu(View view) 31 { 32 mDrawerLayout.openDrawer(Gravity.RIGHT); 33 // mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, 34 // Gravity.RIGHT); //右侧菜单只有点击才能将其弹出 35 } 36 37 private void initEvents() 38 { 39 mDrawerLayout.addDrawerListener(new DrawerListener() 40 { 41 @Override 42 public void onDrawerStateChanged(int newState) 43 { 44 } 45 46 @Override 47 public void onDrawerSlide(View drawerView, float slideOffset) 48 { 49 View mContent = mDrawerLayout.getChildAt(0); 50 View mMenu = drawerView; 51 float scale = 1 - slideOffset; 52 float rightScale = 0.8f + scale * 0.2f; 53 54 if (drawerView.getTag().equals("LEFT")) 55 { 56 57 float leftScale = 1 - 0.3f * scale; 58 59 ViewHelper.setScaleX(mMenu, leftScale); 60 ViewHelper.setScaleY(mMenu, leftScale); 61 ViewHelper.setAlpha(mMenu, 0.6f + 0.4f * (1 - scale)); 62 ViewHelper.setTranslationX(mContent, 63 mMenu.getMeasuredWidth() * (1 - scale)); 64 ViewHelper.setPivotX(mContent, 0); 65 ViewHelper.setPivotY(mContent, 66 mContent.getMeasuredHeight() / 2); 67 mContent.invalidate(); 68 ViewHelper.setScaleX(mContent, rightScale); 69 ViewHelper.setScaleY(mContent, rightScale); 70 } else 71 { 72 ViewHelper.setTranslationX(mContent, 73 -mMenu.getMeasuredWidth() * slideOffset); 74 ViewHelper.setPivotX(mContent, mContent.getMeasuredWidth()); 75 ViewHelper.setPivotY(mContent, 76 mContent.getMeasuredHeight() / 2); 77 mContent.invalidate(); 78 ViewHelper.setScaleX(mContent, rightScale); 79 ViewHelper.setScaleY(mContent, rightScale); 80 } 81 82 } 83 84 @Override 85 public void onDrawerOpened(View drawerView) 86 { 87 } 88 89 @Override 90 public void onDrawerClosed(View drawerView) 91 { 92 // mDrawerLayout.setDrawerLockMode( 93 // DrawerLayout.LOCK_MODE_LOCKED_CLOSED, Gravity.RIGHT); 94 } 95 }); 96 97 98 } 99 100 private void initView()101 {102 mDrawerLayout = (DrawerLayout) findViewById(R.id.id_drawerLayout);103 // mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED,104 // Gravity.RIGHT);105 }106 107 }