一、在XML文件中,先写一个menu的布局,在主布局中通过include将其加载进来(主布局文件:menu+content)
注意:SideslipMenu是继承HorizonScollViewde 类
<RelativeLayout 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" tools:context=".MainActivity"> <com.luanxuye.eecreation.view.SideslipMenu android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="horizontal"> <include layout="@layout/left_menu"/> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:background="#E48A00" android:minHeight="?attr/actionBarSize" android:layout_width="match_parent" android:layout_height="60dp"> <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="首页" android:textSize="35sp" android:textStyle="bold" android:textColor="#fff"/> </android.support.v7.widget.Toolbar> <FrameLayout android:id="@+id/fragment" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1"/> <View android:layout_width="fill_parent" android:layout_height="1dp" android:background="#e2e2e2"/> <LinearLayout android:background="#F9F9F9" android:layout_width="match_parent" android:layout_height="80dp" android:paddingLeft="20dp" android:paddingRight="20dp" android:paddingBottom="5dp" android:paddingTop="5dp" android:orientation="horizontal"> <ImageView android:id="@+id/image_home" android:layout_width="48dp" android:layout_height="70dp" android:src="@drawable/l_home" android:onClick="onClick"/> <View android:layout_width="0dp" android:layout_height="1dp" android:layout_weight="1"/> <ImageView android:id="@+id/image_fenlei" android:layout_width="48dp" android:layout_height="70dp" android:src="@drawable/h_fenlei" android:onClick="onClick"/> <View android:layout_width="0dp" android:layout_height="1dp" android:layout_weight="1"/> <ImageView android:id="@+id/image_message" android:layout_width="48dp" android:layout_height="70dp" android:src="@drawable/h_message" android:onClick="onClick"/> <View android:layout_width="0dp" android:layout_height="1dp" android:layout_weight="1"/> <ImageView android:id="@+id/image_me" android:layout_width="48dp" android:layout_height="70dp" android:src="@drawable/h_me" android:onClick="onClick"/> </LinearLayout> </LinearLayout> </LinearLayout> </com.luanxuye.eecreation.view.SideslipMenu> </RelativeLayout>
二、以下是java文件,注解都在其中
package com.luanxuye.eecreation.view; import android.content.Context; import android.util.AttributeSet; import android.util.DisplayMetrics; import android.util.TypedValue; import android.view.MotionEvent; import android.view.ViewGroup; import android.view.WindowManager; import android.widget.HorizontalScrollView; import android.widget.LinearLayout; /** * Created by 栾绪业 on 2016/02/27. */ public class SideslipMenu extends HorizontalScrollView { /** * 继承HorizontalScrollView的侧滑菜单的实现 * 自定义ViewGroup * 1、onMeasure * 决定内部View(子View)的宽和高以及自己的宽和高 * 2、onLayout * view的放置的位置 * 3、onTouchEvent * 手指按下与释放的时间 */ //必须都有的 private LinearLayout mlaLinearLayout; private ViewGroup mMenu; private ViewGroup mContent; //屏幕的宽度 private int mScreenWidth; private int mMenuWidth ; //菜单距离屏幕右侧的距离 private int mMenuRightPandding = 50; private boolean once = false ; public SideslipMenu(Context context, AttributeSet attrs) { super(context, attrs); //获取屏幕的宽度 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); DisplayMetrics dm = new DisplayMetrics(); //将dm进行赋值 wm.getDefaultDisplay().getMetrics(dm); mScreenWidth = dm.widthPixels; //将屏幕的宽度单位转化成像素(dp--->px) //可以将其封装成为一个工具,用于单位转换 mMenuRightPandding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, context.getResources().getDisplayMetrics()); } /** * 决定内部View(子View)的宽和高以及自己的宽和高 * * @param widthMeasureSpec * @param heightMeasureSpec */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { if (once == false){ mlaLinearLayout = (LinearLayout) getChildAt(0); mMenu = (ViewGroup) mlaLinearLayout.getChildAt(0); mContent = (ViewGroup) mlaLinearLayout.getChildAt(1); //菜单与内容的宽度 mMenuWidth = mMenu.getLayoutParams().width = mScreenWidth - mMenuRightPandding ; mContent.getLayoutParams().width = mScreenWidth ; once = true ; } super.onMeasure(widthMeasureSpec, heightMeasureSpec); } /** * view的放置的位置 *通过偏移量将menu隐藏 */ @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); if (changed){ this.scrollTo(mMenuWidth,0); } } /** * 只需要进行对用户手指松开时的判断 * @param ev * @return */ @Override public boolean onTouchEvent(MotionEvent ev) { int action = ev.getAction() ; switch (action ){ case MotionEvent.ACTION_UP: //隐藏在左边的宽度 int scorllX = getScrollX() ; if (scorllX >= mMenuWidth/2){ this.smoothScrollTo(mMenuWidth,0); }else{ this.smoothScrollTo(0,0); } return true ; } return super.onTouchEvent(ev); } }