侧滑UI

1、视图

activity_main.xml

 1 <com.zyhui.cehua.SlidingMenu 
 2     xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="wrap_content"
 5     android:layout_height="match_parent"
 6     android:id="@+id/menu"
 7     android:background="#ffffff"
 8     android:scrollbars="none"
 9     android:fadingEdge="none"
10      >
11 
12     <LinearLayout
13         android:layout_width="wrap_content"
14         android:layout_height="fill_parent"
15         android:orientation="horizontal" >
16         <include layout="@layout/menu" />
17         <LinearLayout
18             android:layout_width="fill_parent"
19             android:layout_height="fill_parent"
20             android:background="#0000ff"
21             >
22             
23         </LinearLayout>
24     </LinearLayout>
25 
26 </com.zyhui.cehua.SlidingMenu>

menu.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="wrap_content"
 4     android:layout_height="wrap_content"
 5     android:background="#00ff00"
 6     android:orientation="vertical" >
 7     <Button 
 8         android:id="@+id/btn"
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"
11         android:text="abc"
12         />
13 
14 </LinearLayout>

2、清单文件

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.zyhui.cehua"
 4     android:versionCode="1"
 5     android:versionName="1.0" >
 6 
 7     <uses-sdk
 8         android:minSdkVersion="16"
 9         android:targetSdkVersion="16" />
10 
11     <application
12         android:allowBackup="true"
13         android:icon="@drawable/ic_launcher"
14         android:label="@string/app_name"
15         android:theme="@style/AppTheme" >
16         <activity
17             android:name="com.zyhui.cehua.MainActivity"
18             android:label="@string/app_name" 
19             android:theme="@android:style/Theme.NoTitleBar"
20             >
21             <intent-filter>
22                 <action android:name="android.intent.action.MAIN" />
23 
24                 <category android:name="android.intent.category.LAUNCHER" />
25             </intent-filter>
26         </activity>
27     </application>
28 
29 </manifest>

3、MainActivity

 1 package com.zyhui.cehua;
 2 
 3 import android.os.Bundle;
 4 import android.view.View;
 5 import android.view.View.OnClickListener;
 6 import android.widget.Button;
 7 import android.app.Activity;
 8 
 9 public class MainActivity extends Activity {
10     private SlidingMenu sdMenu;
11     private Button btn;
12     @Override
13     protected void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         setContentView(R.layout.activity_main);
16         
17         sdMenu = (SlidingMenu) findViewById(R.id.menu);
18         btn = (Button) findViewById(R.id.btn);
19         btn.setOnClickListener(new OnClickListener() {
20             @Override
21             public void onClick(View v) {
22                 sdMenu.goLeft();
23             }
24         });
25     }
26 
27 }

4、SlidingMenu

 1 package com.zyhui.cehua;
 2 
 3 import android.content.Context;
 4 import android.util.AttributeSet;
 5 import android.util.DisplayMetrics;
 6 import android.view.Display;
 7 import android.view.MotionEvent;
 8 import android.view.ViewGroup;
 9 import android.view.WindowManager;
10 import android.widget.HorizontalScrollView;
11 import android.widget.LinearLayout;
12 
  

//===================================================
//自定义控件的一般套路:
//1、重新父类的构造器
//2、视情况重写onMeasure、onLayout、onDraw和onTouchEvent方法
// onMeasure方法:测试自身及子控件尺寸的.它会调用setMeasureDimension方法
// onLayout方法:对自身及子控件进行布局是调用.它会调用layout方法
//===================================================

13 //HorizontalScrollView可以参考这个http://www.xuebuyuan.com/2162142.html地址
14 public class SlidingMenu extends HorizontalScrollView {
15     private ViewGroup mMenu;
16     private ViewGroup mMain;
17     private int mRightPadding = 0;
18     private int mScreenWidth;
19     private int mMenuWidth;
20     private float downX;
    //为什么要第二个参数的构造器?因为需要xml文件的属性设置载入
21 public SlidingMenu(Context context, AttributeSet attrs) { 22 super(context, attrs); 23 24 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 25 Display display = wm.getDefaultDisplay(); 26 DisplayMetrics outMetrics = new DisplayMetrics(); 27 display.getMetrics(outMetrics); 28 29 //屏幕宽 30 mScreenWidth = outMetrics.widthPixels; 31 } 32 33 //此方法是可以设置控件的宽高
    //什么时候会调用该方法?答:当控件的父元素正要放置该控件时调用.父元素会问子控件一个问题,“你想要用多大地方啊?”
34 @Override 35 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 36 LinearLayout wrapper = (LinearLayout) getChildAt(0); 37 mMenu = (ViewGroup) wrapper.getChildAt(0); 38 mMain = (ViewGroup) wrapper.getChildAt(1); 39 40 //菜单宽度 41 mMenuWidth = mScreenWidth - mRightPadding; 42 mMenu.getLayoutParams().width = mMenuWidth;//设置菜单宽度 43 44 //主界面宽度 45 mMain.getLayoutParams().width = mScreenWidth; 46 47 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 48 } 49 50 @Override 51 public boolean onTouchEvent(MotionEvent ev) { 52 switch(ev.getAction()){ 53 case MotionEvent.ACTION_DOWN: 54 downX = ev.getX(); 55 break; 56 case MotionEvent.ACTION_UP: 57 int d = (int) (ev.getX() - downX); 58 //往左 59 if(d < 0){ 60 this.smoothScrollTo(mMenuWidth, 0); 61 }else{//往右 62 this.smoothScrollTo(0, 0); 63 } 64 break; 65 default: 66 break; 67 } 68 return true; 69 } 70 71 //决定视图的摆放位置
    //onLayout叫做布局方法;它是在调用onMeasure后调用,可以看这个链接的开头:http://blog.csdn.net/yhb5566/article/details/7327075
72 @Override 73 protected void onLayout(boolean changed, int l, int t, int r, int b) { 74 super.onLayout(changed, l, t, r, b); 75 this.scrollBy(mMenuWidth, 0); 76 } 77 78 public void goLeft(){ 79 this.smoothScrollTo(mMenuWidth, 0); 80 } 81 82 }

 更加完善的效果可以参考此人博客:http://www.cnblogs.com/lichenwei/p/4111252.html?utm_source=tuicool&utm_medium=referral

posted @ 2016-05-12 11:34  zhongyinghe  阅读(232)  评论(0)    收藏  举报