slidingMenu

来源:互联网 发布:激光打标机软件配套 编辑:程序博客网 时间:2024/04/30 11:12
继承HorizontalScrollView

自定义ViewGroup
1、onMeasure
决定内部view(子view)的宽和高,以及呢,自己的宽和高
2、onLayout
决定view的 放置的位置
3、onTouchEvent


先简单的建立一个XML文件就是menu菜单的布局
然后创建一个slidingmenu继承自HorizontalScrollView
实现两个参数的构造方法
重写上面三个方法
public class SliddingMenu extends HorizontalScrollView {
 
 private static final String WindowManager = null;
 private LinearLayout mWapper;
 private ViewGroup mMenu;
 private ViewGroup mContent;
 
 private int mScreenWidth;
 
 private int mMenuWidth;
 //dp
 private int mMenuRightPadding = 50;
 
 private boolean once;
 /**
  * 未使用自定义属性时,调用
  * @param context
  * @param attrs
  */
 public SliddingMenu(Context context, AttributeSet attrs) {
  super(context, attrs);
  // TODO Auto-generated constructor stub
  WindowManager wm = (android.view.WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
  DisplayMetrics outMetrics = new DisplayMetrics();
  wm.getDefaultDisplay().getMetrics(outMetrics);
  //获得屏幕的宽度
  mScreenWidth = outMetrics.widthPixels;
 
  //把dp转化为px
  mMenuRightPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, context.getResources().getDisplayMetrics());
 }
 
 /**
  * 设置子view的宽和高,设置自己的宽和高
  */
 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  // TODO Auto-generated method stub
 
  if (!once) {
   mWapper = (LinearLayout) getChildAt(0);
   mMenu = (ViewGroup) mWapper.getChildAt(0);
   mContent = (ViewGroup) mWapper.getChildAt(1);
   
   mMenuWidth = mMenu.getLayoutParams().width = mScreenWidth - mMenuRightPadding;
   mContent.getLayoutParams().width = mScreenWidth;
   once = true;
  }
  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
 
 
 
 }
 
 /**
  * 通过设置偏移量,讲menu隐藏
  */
 @Override
 protected void onLayout(boolean changed, int l, int t, int r, int b) {
  // TODO Auto-generated method stub
  if (!changed) {
   this.smoothScrollTo(mMenuWidth, 0);
  }
  super.onLayout(changed, l, t, r, b);
 }
 
 @Override
 public boolean onTouchEvent(MotionEvent ev) {
  // TODO Auto-generated method stub
  int action = ev.getAction();
  switch (action) {
  case MotionEvent.ACTION_UP:
   int scrollX = getScrollX();
   if (scrollX>=mMenuWidth/2) {
    this.smoothScrollTo(mMenuWidth, 0);
   }else {
    this.smoothScrollTo(0, 0);
   }
   return true;
  }
  return super.onTouchEvent(ev);
 }
}


布局文件
<com.example.sliddingmenudemo.view.SliddingMenu
        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" >
            </include>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/qq" >
            </LinearLayout>
        </LinearLayout>
    </com.example.sliddingmenudemo.view.SliddingMenu>

0 0
原创粉丝点击