首页 / ANDROID / android 实现侧滑菜单
android 实现侧滑菜单
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了android 实现侧滑菜单,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6751字,纯文字阅读大概需要10分钟。
内容图文
![android 实现侧滑菜单](/upload/InfoBanner/zyjiaocheng/1329/aa5afccdf785420cb359562411543976.jpg)
1.主布局
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:background="@drawable/img_frame_background"android:layout_height="match_parent" >
<com.example.slidingview.SlidingView
android:id="@+id/mhsl"android:layout_width="wrap_content"android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/mll"android:layout_width="wrap_content"android:layout_height="match_parent"android:gravity="center"android:orientation="horizontal" >
<include
android:layout_width="match_parent"layout="@layout/left_menu" />
<LinearLayout
android:layout_width="match_parent"android:layout_height="match_parent"android:background="#eeff00">
<TextView
android:id="@+id/mlv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:text="center view" />
</LinearLayout>
</LinearLayout>
</com.example.slidingview.SlidingView>
</RelativeLayout>
2.侧边栏布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="40dp"android:layout_marginTop="150dp"android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/ic_launcher" />
<TextView
android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center"android:text="left view"android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="40dp"android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/ic_launcher" />
<TextView
android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center"android:text="left view"android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="40dp"android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/ic_launcher" />
<TextView
android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center"android:text="left view"android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="40dp"android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/ic_launcher" />
<TextView
android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center"android:text="left view"android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="40dp"android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/ic_launcher" />
<TextView
android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center"android:text="left view"android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="40dp"android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/ic_launcher" />
<TextView
android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center"android:text="left view"android:textSize="30sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
3.自定义view继承horizentalscrollview
public class SlidingView extends HorizontalScrollView{
//我们需要获取三个布局内容
//(1)包裹的整个布局
private LinearLayout mWrapper;
//(2)侧边栏布局private ViewGroup mMenu;
//(3)主界面布局private ViewGroup mItem;
//用于存储整个屏幕的宽度
private
int mScreenWidth;
//菜单滑动之后还能显示的主界面的多少的值privateint mMenuRightPadding;
//已测量的标志,查看是否已测量,让程序不会重复的测量privateboolean once;
//三个构造方法,只调用有三个参数的
public
SlidingView(Context context) {
this(context,null);
// TODO Auto-generated constructor stub
}
publicSlidingView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
publicSlidingView(Context context, AttributeSet attrs) {
this(context, attrs,0);
//获取当前屏幕的宽度
WindowManager wn = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wn.getDefaultDisplay().getMetrics(outMetrics );
mScreenWidth = outMetrics.widthPixels;
//转换成dp
mMenuRightPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 120,
context.getResources().getDisplayMetrics());
}
//自定义view都要用到的onmeasure方法,意在测量
@Override
protected
void
onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stubif(!once){
//获取三个布局
mWrapper = (LinearLayout) getChildAt(0);
mMenu = (ViewGroup) mWrapper.getChildAt(0);
mItem = (ViewGroup) mWrapper.getChildAt(1);
//设置宽和高
mMenuWidth = mMenu.getLayoutParams().width = mScreenWidth-mMenuRightPadding;
mItem.getLayoutParams().width = mScreenWidth;
//标识已测量,不会再重复的测量
once = true;
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
//设置布局
@Override
protected
void
onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stubsuper.onLayout(changed, l, t, r, b);
if(changed){
//默认隐藏侧边栏this.scrollTo(mMenuWidth, 0);
}
}
@Override
public
boolean
onTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub//横向的滑动我们不要监听了,因为horizentalscrollview会帮我们监听,我们只要监听up,手指抬起来的时候判断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);
}
returntrue;
}
returnsuper.onTouchEvent(ev);
}
}
原文:http://blog.csdn.net/a4384142/article/details/45969529
内容总结
以上是互联网集市为您收集整理的android 实现侧滑菜单全部内容,希望文章能够帮你解决android 实现侧滑菜单所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。