第一步,在res的layout里面创建一个侧拉菜单显示的布局,显示的就是你的侧拉菜单上的控件。如下:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mys" android:layout_width="400dp" android:layout_height="match_parent" android:background="#a4cB8F"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <Button android:id="@+id/mytx1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="黑白切换" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="新闻" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="收藏" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="离线缓存" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="浏览记录" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="设置" /> </LinearLayout> </ScrollView>
第二步:在创建一个布局,作为主界面显示:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:background="#741" android:layout_height="match_parent" android:id="@+id/relativelayout" > <Button android:id="@+id/rbutton" android:layout_width="match_parent" android:layout_height="120dp" android:text="主页面切换显示" android:textColor="#000000" android:textSize="30dp" android:gravity="center" /> </RelativeLayout>第三步,在activity_main.xml中,关联上两个布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="text.bawei.com.ryeqiehuanjiacelacaidan.MainActivity"> <text.bawei.com.ryeqiehuanjiacelacaidan.SlideMenu android:id="@+id/slideMenu" android:layout_width="match_parent" android:layout_height="match_parent"> <!--菜单界面的布局--> <include layout="@layout/menu"/> <!--主界面的布局--> <include layout="@layout/activity_layout"/> </text.bawei.com.ryeqiehuanjiacelacaidan.SlideMenu> </RelativeLayout>第四步:创建一个类,继承FrameLayout,用来设置侧拉菜单的属性:
第五步:在main类里调用就可以了:public class SlideMenu extends FrameLayout { private View menuView,mainView; private int menuWidth; private Scroller scroller; public SlideMenu(Context context) { super(context); init(); } public SlideMenu(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init(){ scroller = new Scroller(getContext()); } @Override protected void onFinishInflate() { super.onFinishInflate(); menuView = getChildAt(0); mainView = getChildAt(1); menuWidth = menuView.getLayoutParams().width; } public boolean onInterceptTouchEvent(MotionEvent ev){ switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: downX = (int) ev.getX(); break; case MotionEvent.ACTION_MOVE: int deltaX = (int) (ev.getX() - downX); if (Math.abs(deltaX) > 8){ return true; } break; } return super.onInterceptTouchEvent(ev); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { menuView.layout(-menuWidth, 0, 0, b); mainView.layout(0, 0, r, b); } private int downX; public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: downX = (int) event.getX(); break; case MotionEvent.ACTION_MOVE: int moveX = (int) event.getX(); int deltaX = moveX - downX; int newScrollX = getScrollX() - deltaX; if (newScrollX < -menuWidth) newScrollX = -menuWidth; if (newScrollX > 0) newScrollX = 0; scrollTo(newScrollX, 0); downX = moveX; break; case MotionEvent.ACTION_UP: if(getScrollX()>-menuWidth/2){ closeMenu(); }else { openMenu(); } break; } return true; } private void closeMenu(){ scroller.startScroll(getScrollX(),0,0-getScrollX(),0,400); invalidate(); } private void openMenu(){ scroller.startScroll(getScrollX(),0,-menuWidth-getScrollX(),0,400); invalidate(); } public void computeScroll(){ super.computeScroll(); if(scroller.computeScrollOffset()){ scrollTo(scroller.getCurrX(),0); invalidate(); } } public void switchMenu(){ if(getScrollX()==0){ openMenu(); }else { closeMenu(); } } }
public class MainActivity extends AppCompatActivity { private ImageView btn_back; private SlideMenu slideMenu; private Button day_niight_mode; private RelativeLayout relativeLayout; // private ScrollView ms; // private TextView mytv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); slideMenu= (SlideMenu) findViewById(R.id.slideMenu); day_niight_mode= (Button) findViewById(R.id.rbutton); relativeLayout= (RelativeLayout) findViewById(R.id.relativelayout); //切换黑白 day_niight_mode.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { } }); } }在侧拉菜单里面的布局,如果要设置监听和点击的话,直接添加ID,在main里面找到ID之后,就可以正常使用了,包括主界面的也一样。