Android实现左侧滑动菜单
作者:风云正 时间:2022-10-10 14:58:41
本文实例为大家分享了Android实现左侧滑动菜单的具体代码,供大家参考,具体内容如下
效果图:
SlideActivity.java:
package com.demo.slide;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import com.demo.broadcast.R;
public class SlideActivity extends Activity
{
private SlidingMenu mLeftMenu ;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.slide_main);
mLeftMenu = (SlidingMenu) findViewById(R.id.id_menu);
}
public void toggleMenu(View view)
{
mLeftMenu.toggle();
}
}
SlidingMenu.java:
package com.demo.slide;
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;
public class SlidingMenu extends HorizontalScrollView
{
private LinearLayout mWapper;
private ViewGroup mMenu;
private ViewGroup mContent;
private int mScreenWidth;
private int mMenuWidth;
// dp
private int mMenuRightPadding = 80;
private boolean once;
private boolean isOpen;
/**
* 未使用自定义属性时,调用
*
* @param context
* @param attrs
*/
public SlidingMenu(Context context, AttributeSet attrs)
{
this(context, attrs, 0);
}
/**
* 当使用了自定义属性时,会调用此构造方法
*
* @param context
* @param attrs
* @param defStyle
*/
public SlidingMenu(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
mScreenWidth = outMetrics.widthPixels;
mMenuRightPadding = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 50, context
.getResources().getDisplayMetrics());
}
public SlidingMenu(Context context)
{
this(context, null);
}
/**
* 设置子View的宽和高 设置自己的宽和高
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
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)
{
super.onLayout(changed, l, t, r, b);
if (changed)
{
this.scrollTo(mMenuWidth, 0);
}
}
@Override
public boolean onTouchEvent(MotionEvent ev)
{
int action = ev.getAction();
switch (action)
{
case MotionEvent.ACTION_UP:
// 隐藏在左边的宽度
int scrollX = getScrollX();
if (scrollX >= mMenuWidth / 2)
{
this.smoothScrollTo(mMenuWidth, 0);
isOpen = false;
} else
{
this.smoothScrollTo(0, 0);
isOpen = true;
}
return true;
}
return super.onTouchEvent(ev);
}
/**
* 打开菜单
*/
public void openMenu()
{
if (isOpen)
return;
this.smoothScrollTo(0, 0);
isOpen = true;
}
public void closeMenu()
{
if (!isOpen)
return;
this.smoothScrollTo(mMenuWidth, 0);
isOpen = false;
}
/**
* 切换菜单
*/
public void toggle()
{
if (isOpen)
{
closeMenu();
} else
{
openMenu();
}
}
}
slide_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.demo.slide.SlidingMenu
android:id="@+id/id_menu"
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:background="#ffffff" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="toggleMenu"
android:text="切换" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:text="我是主content"
android:textColor="#ff00ff"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
</com.demo.slide.SlidingMenu>
</RelativeLayout>
left_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="我是左侧Menu"
android:textColor="#ffffff"
android:textSize="20sp" />
</LinearLayout>
</RelativeLayout>
来源:https://blog.csdn.net/chenzheng8975/article/details/84690431
标签:Android,滑动,菜单
0
投稿
猜你喜欢
android实现欢迎界面效果
2021-11-05 14:44:52
Java实现SMS短信通发送手机验证码案例讲解
2022-05-14 22:37:57
实例详解C#正则表达式
2023-06-23 00:03:03
Android EditText追加空格、限制字符等方法示例
2023-10-02 17:10:47
java数据结构基础:单链表与双向链表
2023-03-02 09:21:59
MyBatis在SQL语句中如何获取list的大小
2021-08-15 12:09:55
WCF实现的计算器功能实例
2022-05-07 23:17:12
Springboot导出文件,前端下载文件方式
2023-07-21 11:27:05
分布式医疗挂号系统SpringCache与Redis为数据字典添加缓存
2023-06-28 02:26:55
IDEA JeeSite框架httpSession.invalidate()无效问题解决方案
2023-09-23 19:44:24
Android实现多段颜色进度条效果
2023-09-30 22:07:51
Android 3D滑动菜单完全解析 Android实现推拉门式的立体特效
2022-04-19 04:36:01
C#类继承中构造函数的执行序列示例详解
2022-05-26 11:42:09
使用位运算实现网页中的过滤、筛选功能实例
2023-06-04 10:00:31
Kotlin使用协程实现高效并发程序流程详解
2021-12-27 20:47:21
Java版画板的实现方法
2023-01-06 22:31:44
C#净化版WebApi框架的实现
2021-10-31 03:59:09
Java虚拟机栈jvm栈的作用
2023-11-10 13:52:15
Android使用setContentView实现页面的转换效果
2021-07-30 15:13:32
Java中的Gradle与Groovy的区别及存在的关系
2023-05-13 17:28:46