当前位置:   article > 正文

Android简单的自定义抽屉布局(DrawerLayout)_android 自定义抽屉布局

android 自定义抽屉布局

采用DrawerLayout的方法实现抽屉布局的效果,抽屉布局有两个部分,第一个部分为主内容区,如图中的包含底部菜单栏的部分。第二部分为侧边栏部分,左边部分。

对应xml布局如下:

  1. <android.support.v4.widget.DrawerLayout
  2. android:id="@+id/drawer_main"
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:background="#FFFFFF"
  7. >
  8. <RelativeLayout
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent"
  11. android:background="#eeeeee"
  12. >
  13. <include
  14. android:id="@+id/include2"
  15. layout="@layout/top_search_view"
  16. android:layout_width="match_parent"
  17. android:layout_height="100px"
  18. android:layout_alignParentTop="true"
  19. ></include>
  20. <FrameLayout
  21. android:id="@+id/fl_drawerlayout"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:layout_below="@+id/include2"
  25. android:layout_above="@+id/textview"
  26. />
  27. <TextView
  28. android:id="@+id/textview"
  29. android:layout_width="match_parent"
  30. android:layout_height="1dp"
  31. android:background="#bfbfbf"
  32. android:layout_above="@+id/include"
  33. android:layout_alignParentStart="true" />
  34. <include
  35. android:id="@+id/include"
  36. layout="@layout/bottom_menu"
  37. android:layout_width="match_parent"
  38. android:layout_height="100px"
  39. android:layout_alignParentBottom="true"
  40. ></include>
  41. </RelativeLayout>
  42. <LinearLayout
  43. android:id="@+id/ll_left_menus"
  44. android:layout_height="wrap_content"
  45. android:layout_gravity="start"
  46. android:layout_width="250dp"
  47. android:orientation="vertical">
  48. <include
  49. layout="@layout/left_menu"
  50. android:layout_height="match_parent"
  51. android:layout_width="wrap_content"
  52. >
  53. </include>
  54. </LinearLayout>
  55. </android.support.v4.widget.DrawerLayout>

其中的RelativeLayout布局所包含的内容即为主内容,其中第一个include进的布局为标题栏,类似于actionbar,第二个include进的为底部菜单栏,其中的FrameLayout用来填充fragment,点击对应的底部菜单后添加对应的fragment。

LinearLayout布局则对应的是侧边栏,include进来的是一个自定义布局,包括头像,名字,功能列表等。其中layout_gravity属性的值设为start则表示侧边栏从左往右滑动开启,设置为end则表示从右往左滑动开启。



类文件如下:

  1. package com.xiaoyi.Main;
  2. import android.app.Activity;
  3. import android.app.Fragment;
  4. import android.app.FragmentManager;
  5. import android.graphics.Bitmap;
  6. import android.graphics.Canvas;
  7. import android.graphics.Paint;
  8. import android.graphics.PorterDuff;
  9. import android.graphics.PorterDuffXfermode;
  10. import android.graphics.RectF;
  11. import android.os.Bundle;
  12. import android.support.v4.view.GravityCompat;
  13. import android.support.v4.widget.DrawerLayout;
  14. import android.util.Log;
  15. import android.view.View;
  16. import android.widget.ImageView;
  17. import android.widget.TextView;
  18. import com.xiaoyi.Company.CompanyFragment;
  19. import com.xiaoyi.Friend.FriendFragment;
  20. import com.xiaoyi.Message.MessageFragment;
  21. import com.xiaoyi.position.PositionFragment;
  22. public class MainActivity extends Activity{
  23. private ImageView iv_head,iv_left_setting; //用户头像和设置按钮
  24. private ImageView iv_bottom_menu0,iv_bottom_menu1,iv_bottom_menu2,iv_bottom_menu3;//底部菜单
  25. private ImageView iv_left_menu0,iv_left_menu1,iv_left_menu2,iv_left_menu3; //抽屉中的菜单
  26. private TextView tv_top_name; //顶部标题栏文字
  27. private ImageView iv_search; //搜索按钮
  28. private ImageView iv_drawer; //开启抽屉的点击图标
  29. private DrawerLayout drawer_main; //抽屉
  30. @Override
  31. protected void onCreate(Bundle savedInstanceState) {
  32. super.onCreate(savedInstanceState);
  33. setContentView(R.layout.activity_main);
  34. initView();
  35. }
  36. public void initView(){
  37. iv_head = (ImageView)findViewById(R.id.iv_head);
  38. iv_left_setting = (ImageView)findViewById(R.id.iv_left_setting);
  39. iv_bottom_menu0 = (ImageView)findViewById(R.id.iv_bottom_menu0);
  40. iv_bottom_menu1 = (ImageView)findViewById(R.id.iv_bottom_menu1);
  41. iv_bottom_menu2 = (ImageView)findViewById(R.id.iv_bottom_menu2);
  42. iv_bottom_menu3 = (ImageView)findViewById(R.id.iv_bottom_menu3);
  43. iv_left_menu0 = (ImageView)findViewById(R.id.iv_left_menu0);
  44. iv_left_menu1 = (ImageView)findViewById(R.id.iv_left_menu1);
  45. iv_left_menu2 = (ImageView)findViewById(R.id.iv_left_menu2);
  46. iv_left_menu3 = (ImageView)findViewById(R.id.iv_left_menu3);
  47. tv_top_name = (TextView)findViewById(R.id.tv_top_name);
  48. iv_search = (ImageView)findViewById(R.id.iv_search);
  49. BottomMenuOnclick();//底部菜单的点击事件监听
  50. LeftMenuOnclick(); //侧边栏菜单的点击事件的监听
  51. drawer_main = (DrawerLayout) findViewById(R.id.drawer_main);
  52. iv_drawer = (ImageView)findViewById(R.id.iv_drawer);
  53. iv_drawer.setOnClickListener(new View.OnClickListener() {
  54. @Override
  55. public void onClick(View v) {
  56. drawer_main.openDrawer(GravityCompat.START); //点击按钮后打开抽屉,START为从左向右打开,END为从右向左打开,跟xml布局中的属性一直
  57. }
  58. });
  59. }
  60. public void LeftMenuOnclick(){
  61. iv_left_menu0.setOnClickListener(new View.OnClickListener() {
  62. @Override
  63. public void onClick(View v) {
  64. }
  65. });
  66. iv_left_menu1.setOnClickListener(new View.OnClickListener() {
  67. @Override
  68. public void onClick(View v) {
  69. }
  70. });
  71. iv_left_menu2.setOnClickListener(new View.OnClickListener() {
  72. @Override
  73. public void onClick(View v) {
  74. }
  75. });
  76. iv_left_menu3.setOnClickListener(new View.OnClickListener() {
  77. @Override
  78. public void onClick(View v) {
  79. }
  80. });
  81. }
  82. public void BottomMenuOnclick(){
  83. //初始时为职位fragment页面
  84. iv_bottom_menu0.setBackgroundResource(R.drawable.position_select);
  85. Fragment positionFragment = new PositionFragment();
  86. FragmentManager fm = getFragmentManager();
  87. fm.beginTransaction().replace(R.id.fl_drawerlayout,positionFragment).commit();
  88. tv_top_name.setText("职位");
  89. iv_bottom_menu0.setOnClickListener(new View.OnClickListener() {
  90. @Override
  91. public void onClick(View v) {
  92. iv_bottom_menu0.setBackgroundResource(R.drawable.position_select);
  93. iv_bottom_menu1.setBackgroundResource(R.drawable.company_no_select);
  94. iv_bottom_menu2.setBackgroundResource(R.drawable.message_no_select);
  95. iv_bottom_menu3.setBackgroundResource(R.drawable.friend_no_select);
  96. Fragment positionFragment = new PositionFragment();
  97. FragmentManager fm = getFragmentManager();
  98. fm.beginTransaction().replace(R.id.fl_drawerlayout, positionFragment).commit();
  99. tv_top_name.setText("职位");
  100. iv_search.setVisibility(View.VISIBLE);
  101. }
  102. });
  103. iv_bottom_menu1.setOnClickListener(new View.OnClickListener() {
  104. @Override
  105. public void onClick(View v) {
  106. iv_bottom_menu0.setBackgroundResource(R.drawable.position_no_select);
  107. iv_bottom_menu1.setBackgroundResource(R.drawable.company_select);
  108. iv_bottom_menu2.setBackgroundResource(R.drawable.message_no_select);
  109. iv_bottom_menu3.setBackgroundResource(R.drawable.friend_no_select);
  110. Fragment companyFragment = new CompanyFragment();
  111. FragmentManager fm = getFragmentManager();
  112. fm.beginTransaction().replace(R.id.fl_drawerlayout, companyFragment).commit();
  113. tv_top_name.setText("企业");
  114. iv_search.setVisibility(View.VISIBLE);
  115. }
  116. });
  117. iv_bottom_menu2.setOnClickListener(new View.OnClickListener() {
  118. @Override
  119. public void onClick(View v) {
  120. iv_bottom_menu0.setBackgroundResource(R.drawable.position_no_select);
  121. iv_bottom_menu1.setBackgroundResource(R.drawable.company_no_select);
  122. iv_bottom_menu2.setBackgroundResource(R.drawable.message_select);
  123. iv_bottom_menu3.setBackgroundResource(R.drawable.friend_no_select);
  124. Fragment messageFragment = new MessageFragment();
  125. FragmentManager fm = getFragmentManager();
  126. fm.beginTransaction().replace(R.id.fl_drawerlayout, messageFragment).commit();
  127. tv_top_name.setText("消息");
  128. iv_search.setVisibility(View.INVISIBLE);
  129. }
  130. });
  131. iv_bottom_menu3.setOnClickListener(new View.OnClickListener() {
  132. public void onClick(View v) {
  133. iv_bottom_menu0.setBackgroundResource(R.drawable.position_no_select);
  134. iv_bottom_menu1.setBackgroundResource(R.drawable.company_no_select);
  135. iv_bottom_menu2.setBackgroundResource(R.drawable.message_no_select);
  136. iv_bottom_menu3.setBackgroundResource(R.drawable.friend_select);
  137. Fragment friendFragment = new FriendFragment();
  138. FragmentManager fm = getFragmentManager();
  139. fm.beginTransaction().replace(R.id.fl_drawerlayout, friendFragment).commit();
  140. tv_top_name.setText("朋友");
  141. iv_search.setVisibility(View.INVISIBLE);
  142. }
  143. });
  144. }
  145. }


由于不知道什么原因,如果你用activity直接继承OnClickListener()的方式监听点击事件的话并没有效果,因此本人采用直接设置监听器的方式来监听点击事件。

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号