现在实现侧边栏比较简单了,官方提供的DrawerLayout可以很方便实现。 
主要实现方法是:用DrawerLayout 作为界面根控件。在DrawerLayout里面第一个View为当前界面主内容;第二个和第三个View为抽屉菜单内容。如果当前界面只需要一个抽屉菜单,则第三个View可以省略。 
第一个View的宽高应当设置为match_parent 
第二、三个View需要设置android:layout_gravity=”left”,和android:layout_gravity=”right”且一搬高度设置为match_parent,宽度为固定值,即侧滑菜单的宽度,宽度应该不超过320dp,这样用户可以在菜单打开的时候看到部分内容界面。如果需要监听菜单打开关闭事件,则需要调用 DrawerLayout类的setDrawerListener() 
该接口提供了菜单打开关闭等事件的回调函数,例如 
onDrawerOpened() 
和 
onDrawerClosed() 
效果图: 
 
描述一下:右侧滑出侧边栏,包括一个标题,一个车辆列表,点击列表,右侧滑出一个与侧边栏等宽的popupwindow,也包括标题与列表,点击popupwindow消失。主要有两点,第一是侧边栏,第二是popupwindow以及滑动动画 
上代码: 
主布局activity_favorite_car.xml

  1. <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:id="@+id/id_drawerLayout"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:background="#f2f2f2">
  7. <LinearLayout
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. android:orientation="vertical">
  11. <boerpower.com.boerchargingpile.UI.CustomTitles.TitleLayout
  12. android:id="@+id/id_titleLayot"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:background="@mipmap/top">
  16. </boerpower.com.boerchargingpile.UI.CustomTitles.TitleLayout>
  17. <LinearLayout
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content"
  20. android:layout_marginBottom="10dp"
  21. android:layout_marginTop="10dp"
  22. android:background="#ffffff"
  23. android:orientation="vertical">
  24. <ImageView
  25. android:id="@+id/id_imageMyCar"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:layout_gravity="center"
  29. android:layout_marginTop="88dp"
  30. android:layout_marginBottom="88dp"
  31. android:src="@mipmap/mycar_2" />
  32. </LinearLayout>
  33. <LinearLayout
  34. android:id="@+id/id_linearCarKind"
  35. android:layout_width="match_parent"
  36. android:layout_height="wrap_content"
  37. android:orientation="horizontal"
  38. android:background="@drawable/rect_white"
  39. android:padding="10dp"
  40. android:gravity="center">
  41. <TextView
  42. android:layout_width="0dp"
  43. android:layout_height="wrap_content"
  44. android:textSize="14sp"
  45. android:layout_weight="1"
  46. android:text="@string/mycar_kind"
  47. android:drawableLeft="@mipmap/mycar"
  48. android:drawablePadding="10dp"
  49. android:textColor="#3a3a3a"/>
  50. <ImageView
  51. android:layout_width="wrap_content"
  52. android:layout_height="wrap_content"
  53. android:src="@mipmap/next_arrow"
  54. />
  55. </LinearLayout>
  56. </LinearLayout>
  57. <LinearLayout
  58. android:id="@+id/id_right_menu"
  59. android:layout_width="240dp"
  60. android:layout_height="match_parent"
  61. android:layout_gravity="right"
  62. android:orientation="vertical">
  63. <LinearLayout
  64. android:layout_width="match_parent"
  65. android:layout_height="wrap_content"
  66. android:gravity="center"
  67. android:background="@mipmap/top"
  68. android:orientation="horizontal">
  69. <ImageView
  70. android:id="@+id/id_imageUp"
  71. android:layout_width="wrap_content"
  72. android:layout_height="wrap_content"
  73. android:src="@mipmap/back"
  74. android:layout_margin="12dp"/>
  75. <TextView
  76. android:layout_width="0dp"
  77. android:layout_height="wrap_content"
  78. android:layout_weight="1"
  79. android:gravity="center"
  80. android:text="@string/mycar_favorite"
  81. android:textColor="#ffffff"
  82. android:textSize="17sp" />
  83. </LinearLayout>
  84. <ListView
  85. android:id="@+id/id_listView"
  86. android:layout_width="match_parent"
  87. android:layout_height="match_parent"
  88. android:background="#ffffff"
  89. android:cacheColorHint="#00000000"
  90. android:divider="#f2f2f2"
  91. android:dividerHeight="1dp"/>
  92. </LinearLayout>
  93. </android.support.v4.widget.DrawerLayout>

主界面代码

  1. //我的爱车
  2. public class FavoriteCarActivity extends BaseActivity implements TitleLayout.titleLayoutClick {
  3. private TitleLayout titleLayout;
  4. private ImageView imageViewCar;
  5. private LinearLayout linearLayoutCarKind;
  6. private LinearLayout linearLayoutRight;//右边栏
  7. private DrawerLayout mDrawerLayout;
  8. private ListView listViewCar;//车名称
  9. private ArrayList<String> carNameList = new ArrayList<>();//车名称
  10. private ArrayList<String> carModelList = new ArrayList<>();//车类型
  11. private PopupWindow popupWindow = null;
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_favorite_car);
  16. titleLayout = (TitleLayout) findViewById(R.id.id_titleLayot);
  17. titleLayout.setTitle("我的爱车");
  18. imageViewCar = (ImageView) findViewById(R.id.id_imageMyCar);
  19. ImageView imageViewUp = (ImageView) findViewById(R.id.id_imageUp);
  20. linearLayoutCarKind = (LinearLayout) findViewById(R.id.id_linearCarKind);
  21. linearLayoutRight = (LinearLayout) findViewById(R.id.id_right_menu);
  22. linearLayoutCarKind.setOnClickListener(new View.OnClickListener() {
  23. @Override
  24. public void onClick(View v) {
  25. OpenRightMenu();
  26. }
  27. });
  28. mDrawerLayout = (DrawerLayout) findViewById(R.id.id_drawerLayout);
  29. mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED,
  30. Gravity.RIGHT);
  31. listViewCar = (ListView) findViewById(R.id.id_listView);
  32. for (int i = 0; i < 10; i++) {
  33. carNameList.add("保时捷");
  34. carModelList.add("Panamera");
  35. }
  36. CarAdapter carAdapter = new CarAdapter();
  37. listViewCar.setAdapter(carAdapter);
  38. listViewCar.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  39. @Override
  40. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  41. showCarModelPopupWindow();
  42. }
  43. });
  44. imageViewUp.setOnClickListener(new View.OnClickListener() {
  45. @Override
  46. public void onClick(View v) {
  47. mDrawerLayout.closeDrawers();
  48. }
  49. });
  50. }
  51. public void OpenRightMenu() {
  52. mDrawerLayout.openDrawer(Gravity.RIGHT);
  53. mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED,
  54. Gravity.RIGHT);
  55. }
  56. private class CarAdapter extends BaseAdapter {
  57. public CarAdapter() {
  58. }
  59. @Override
  60. public int getCount() {
  61. return carNameList.size();
  62. }
  63. @Override
  64. public Object getItem(int position) {
  65. return position;
  66. }
  67. @Override
  68. public long getItemId(int position) {
  69. return position;
  70. }
  71. @Override
  72. public View getView(int position, View convertView, ViewGroup parent) {
  73. if (convertView == null) {
  74. convertView = LayoutInflater.from(FavoriteCarActivity.this).inflate(R.layout.car_name_item, null);
  75. }
  76. TextView textViewCarName = ViewHolder.get(convertView, R.id.id_textViewCar);
  77. textViewCarName.setText(carNameList.get(position));
  78. return convertView;
  79. }
  80. }
  81. @Override
  82. public void leftViewClick() {
  83. finish();
  84. }
  85. @Override
  86. public void rightViewClick() {
  87. }
  88. private void showCarModelPopupWindow() {
  89. View view = View.inflate(FavoriteCarActivity.this, R.layout.car_model_list, null);
  90. ImageView imageViewUp = (ImageView) view.findViewById(R.id.id_imageUp);
  91. TextView textViewTitle = (TextView) view.findViewById(R.id.id_textViewCarName);
  92. ListView listViewCarModel = (ListView) view.findViewById(R.id.id_listViewCarModel);
  93. popupWindow = new PopupWindow(view, linearLayoutRight.getWidth(),
  94. LinearLayout.LayoutParams.MATCH_PARENT, true);
  95. popupWindow.setAnimationStyle(R.style.MenuAnimationLeftRight);
  96. popupWindow.setBackgroundDrawable(new BitmapDrawable());//需要设置背景,用物理键返回的时候
  97. popupWindow.setFocusable(true);
  98. popupWindow.setOutsideTouchable(true); //设置点击屏幕其它地方弹出框消失
  99. CarModelAdapter carModelAdapter = new CarModelAdapter();
  100. listViewCarModel.setAdapter(carModelAdapter);
  101. listViewCarModel.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  102. @Override
  103. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  104. popupWindow.dismiss();
  105. }
  106. });
  107. popupWindow.showAtLocation(titleLayout, Gravity.RIGHT, 0, 0);
  108. imageViewUp.setOnClickListener(new View.OnClickListener() {
  109. @Override
  110. public void onClick(View v) {
  111. popupWindow.dismiss();
  112. }
  113. });
  114. }
  115. private class CarModelAdapter extends BaseAdapter {
  116. public CarModelAdapter() {
  117. }
  118. @Override
  119. public int getCount() {
  120. return carModelList.size();
  121. }
  122. @Override
  123. public Object getItem(int position) {
  124. return position;
  125. }
  126. @Override
  127. public long getItemId(int position) {
  128. return position;
  129. }
  130. @Override
  131. public View getView(int position, View convertView, ViewGroup parent) {
  132. if (convertView == null) {
  133. convertView = LayoutInflater.from(FavoriteCarActivity.this).inflate(R.layout.car_model_item, null);
  134. }
  135. TextView textViewCarName = ViewHolder.get(convertView, R.id.id_textViewCarModel);
  136. textViewCarName.setText(carModelList.get(position));
  137. return convertView;
  138. }
  139. }
  140. @Override
  141. public boolean onKeyDown(int keyCode, KeyEvent event) {
  142. if (keyCode == KeyEvent.KEYCODE_BACK) {
  143. if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) {
  144. mDrawerLayout.closeDrawers();
  145. } else {
  146. return super.onKeyDown(keyCode, event);
  147. }
  148. return true;
  149. }
  150. return super.onKeyDown(keyCode, event);
  151. }
  152. }

标题类TitleLayout.java

  1. /**
  2. * Created by dell on 2015/11/10.
  3. * 布局标题
  4. */
  5. public class TitleLayout extends RelativeLayout {
  6. TextView textViewContent;
  7. LinearLayout linearLeft;
  8. ImageView imageViewLeft;
  9. LinearLayout linearRight;
  10. ImageView imageViewRight;
  11. TextView textViewRight;
  12. private titleLayoutClick mListener;
  13. public TitleLayout(Context context, AttributeSet attrs) {
  14. super(context, attrs);
  15. LayoutInflater.from(context).inflate(R.layout.title, this);
  16. textViewContent = (TextView) findViewById(R.id.id_textviewContent);
  17. linearLeft = (LinearLayout) findViewById(R.id.id_linearLeft);
  18. imageViewLeft = (ImageView)findViewById(R.id.id_left);
  19. linearRight = (LinearLayout) findViewById(R.id.id_linearRight);
  20. imageViewRight = (ImageView)findViewById(R.id.id_right);
  21. textViewRight = (TextView) findViewById(R.id.id_rightText);
  22. try{
  23. mListener = (titleLayoutClick) context;
  24. }catch (Exception e){
  25. e.printStackTrace();
  26. }
  27. linearLeft.setOnClickListener(new OnClickListener() {
  28. @Override
  29. public void onClick(View v) {
  30. if (mListener != null) {
  31. mListener.leftViewClick();
  32. }
  33. }
  34. });
  35. linearRight.setOnClickListener(new OnClickListener() {
  36. @Override
  37. public void onClick(View v) {
  38. if (mListener != null) {
  39. mListener.rightViewClick();
  40. }
  41. }
  42. });
  43. }
  44. public void setTitle(String title) {
  45. textViewContent.setText(title);
  46. }
  47. //设置右边图片
  48. public void setLinearRightImage(int drawableID) {
  49. imageViewRight.setBackgroundResource(drawableID);
  50. }
  51. //设置左边图片
  52. public void setLinearLeftImage(int drawableID) {
  53. imageViewLeft.setBackgroundResource(drawableID);
  54. }
  55. //设置右边文本
  56. public void setLinearRightText(String rightTextString) {
  57. textViewRight.setText(rightTextString);
  58. }
  59. //设置右边图片为可见,根据viewID判断文本与图片的显示,0显示图片,1显示文本
  60. public void setLinearRightVisibility(int viewID) {
  61. linearRight.setVisibility(View.VISIBLE);
  62. if(viewID==0){
  63. imageViewRight.setVisibility(View.VISIBLE);
  64. textViewRight.setVisibility(View.INVISIBLE);
  65. }else if(viewID==1){
  66. textViewRight.setVisibility(View.VISIBLE);
  67. imageViewRight.setVisibility(View.INVISIBLE);
  68. }
  69. }
  70. public interface titleLayoutClick {
  71. public void leftViewClick();//左图片点击
  72. public void rightViewClick();//右边图片点击
  73. }
  74. }

标题类布局title.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/id_top"
  4. android:layout_width="match_parent"
  5. android:layout_height="wrap_content">
  6. <LinearLayout
  7. android:id="@+id/id_linearLeft"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:layout_alignParentLeft="true"
  11. android:layout_centerVertical="true">
  12. <ImageView
  13. android:id="@+id/id_left"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:layout_margin="12dp"
  17. android:background="@mipmap/back" />
  18. </LinearLayout>
  19. <LinearLayout
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:layout_centerInParent="true">
  23. <TextView
  24. android:id="@+id/id_textviewContent"
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:layout_margin="12dp"
  28. android:textColor="@color/white"
  29. android:textSize="17sp"/>
  30. </LinearLayout>
  31. <LinearLayout
  32. android:id="@+id/id_linearRight"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:layout_alignParentRight="true"
  36. android:layout_centerVertical="true"
  37. android:visibility="invisible">
  38. <ImageView
  39. android:id="@+id/id_right"
  40. android:layout_width="wrap_content"
  41. android:layout_height="wrap_content"
  42. android:layout_margin="12dp" />
  43. <TextView
  44. android:id="@+id/id_rightText"
  45. android:layout_width="wrap_content"
  46. android:layout_height="wrap_content"
  47. android:layout_margin="12dp"
  48. android:textSize="14sp"
  49. android:textColor="#ffffff"/>
  50. </LinearLayout>
  51. </RelativeLayout>

列表项布局就不写了 
动画style

  1. <!-- PopupWindow左右滑动的窗口动画 -->
  2. <style name="MenuAnimationLeftRight">
  3. <item name="android:windowEnterAnimation">@anim/menu_fadein_right</item>
  4. <item name="android:windowExitAnimation">@anim/menu_fadein_left</item>
  5. </style>

menu_fadein_right

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">
  3. <translate
  4. android:duration="500"
  5. android:fromXDelta="100%"
  6. android:toXDelta="0"
  7. />
  8. </set>

menu_fadein_left

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">
  3. <translate
  4. android:duration="500"
  5. android:fromXDelta="0"
  6. android:toXDelta="100%" />
  7. </set>

参考博客 
DrawerLayout 抽屉 
Android DrawerLayout 高仿QQ5.2双向侧滑菜单

android DrawerLayout 侧边栏实现的更多相关文章

  1. Android DrawerLayout 抽屉

    Android DrawerLayout 抽屉 DrawerLayout 在supportV4 Lib在.类似的开源slidemenu如,DrawerLayout父类ViewGroup,自定义组件基本 ...

  2. Android - DrawerLayout

    Android DrawerLayout 的使用 Android L Android Studio 1.4 从主视图左侧能抽出一个导航栏,效果图:  点击后弹出新界面:  新界面也可以抽出左侧导航栏 ...

  3. Android App 侧边栏菜单的简单实现

    效果图 Layout 注意事项 想要实现侧边栏,需要配合使用DrawerLayout.因为会用到嵌套布局,所以根布局不能是 ConstraintLayout,最好使用 LinearLayout 布局. ...

  4. Android实现侧边栏SlidingPaneLayout

    //主布局 1 <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widg ...

  5. Android DrawerLayout Plus 增强版抽屉菜单

    DrawerLayout是官方提供的侧滑菜单,相比SliddingMenu,它更加轻量级.默认情况下,DrawerLayout可以设置左侧或者右侧滑出菜单.如下,   xml布局: <!-- & ...

  6. Android DrawerLayout 高仿QQ5.2双向侧滑菜单

    1.概述 之前写了一个Android 高仿 QQ5.0 侧滑菜单效果 自定义控件来袭 ,恰逢QQ5.2又加了一个右侧菜单,刚好看了下DrawerLayout,一方面官方的东西,我都比较感兴趣:另一方面 ...

  7. Android DrawerLayout 点击事情穿透

    今天使用DrawerLayout做网易4.4版本侧边栏发现点击DrawerLayout空白部分,下面部分content会获得点击事件.解决方法是在<!-- The navigation draw ...

  8. Android: DrawerLayout 侧滑菜单栏

    DrawerLayout是SupportLibrary包中实现的侧滑菜单效果的控件. 分为主内容区域和侧边菜单区域 drawerLayout本身就支持:侧边菜单根据手势展开与隐藏, 开发者只需要实现: ...

  9. Android DrawerLayout侧滑菜单

    本教程已经录制视频,欢迎大家观看我在CSDN学院录制的课程:http://edu.csdn.net/lecturer/944

随机推荐

  1. C# 随笔 【ToList().Foreach()和Foreach()】

    1. 最近在做一个Socket通讯的例子,但是如果使用UTF-8编码传输中文的话取和的会不一样.早上做了测试 . string str = "a我..";看代码中间是一个英文,一个 ...

  2. css如何将div画成三角形

    首先了解一下盒模型: 盒模型 先看一段代码: #div1{ height: 100px; border-style: solid; border-width: 100px 100px 100px 10 ...

  3. POJ 3186Treats for the Cows(区间DP)

    题目链接:http://poj.org/problem?id=3186 题目大意:给出的一系列的数字,可以看成一个双向队列,每次只能从队首或者队尾出队,第n个出队就拿这个数乘以n,最后将和加起来,求最 ...

  4. 关于ZIP自动打包的进一步进化思路

    http://blog.163.com/long200259@126/blog/static/11288755920093120529157/

  5. 洛谷P1615 西游记公司 题解

    题目传送门 这道题题面写得非常好. 但好像并没有什么ruan用. 这道题貌似就是把时间差求出来,乘上猪八戒能偷的电脑数就好了.(注意long long) #include<bits/stdc++ ...

  6. 小程序授权怎么写 , 用户点击取消授权 调用 wx.authorize

    点击获取授权 onLoad: function (options) { console.log("onLoad====="); var that=this; wx.getUserI ...

  7. javascript copy text to clipboard

    本段代码摘自微软docs网站上,目前需要解决在IE浏览器中触发copy事件的方法,也可以直接调用jquery. <!DOCTYPE html> <html> <head& ...

  8. LoadRunner中Vugen-Recording Options选项卡介绍:

    LoadRunner中Vugen-Recording Options选项卡介绍:

  9. Redis Cluster集群的搭建与实践

    Redis Cluster集群 一.redis-cluster设计 Redis集群搭建的方式有多种,例如使用zookeeper等,但从redis 3.0之后版本支持redis-cluster集群,Re ...

  10. 使用fastadmin系统自带的图片上传plupload

    首先,form表单需要具有如下代码 <form class="form-horizontal" role="form" method="POST ...