最简单的slidingMenu
来源:互联网 发布:iphone制作视频软件 编辑:程序博客网 时间:2024/06/05 09:30
直接代码
1、 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="#0000" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/id_img1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:src="@drawable/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@id/id_img1"
android:text="第一个ITEM"
android:textColor="#ffffff"
android:textSize="20dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/id_img2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:src="@drawable/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@id/id_img2"
android:text="第二个ITEM"
android:textColor="#ffffff"
android:textSize="20dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/id_img3"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:src="@drawable/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@id/id_img3"
android:text="第三个ITEM"
android:textColor="#ffffff"
android:textSize="20dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/id_img4"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:src="@drawable/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@id/id_img4"
android:text="第四个ITEM"
android:textColor="#ffffff"
android:textSize="20dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/id_img5"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:src="@drawable/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@id/id_img5"
android:text="第五个ITEM"
android:textColor="#ffffff"
android:textSize="20dp" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
2、main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.example.sliding.SldingMenu
android:id="@+id/menu"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
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:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:text="切换菜单" />
</LinearLayout>
</LinearLayout>
</com.example.sliding.SldingMenu>
</RelativeLayout>
3、public class SldingMenu extends HorizontalScrollView {
private int ScreenWidth;
private ViewGroup LeftMenu;
private ViewGroup ContentMenu;
private LinearLayout mWapper;
private boolean once;
private int leftMenuWidth;
public SldingMenu(Context context, AttributeSet attrs) {
super(context, attrs);
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
ScreenWidth = outMetrics.widthPixels;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (!once) {
mWapper = (LinearLayout) getChildAt(0);
LeftMenu = (ViewGroup) mWapper.getChildAt(0);
ContentMenu = (ViewGroup) mWapper.getChildAt(1);
leftMenuWidth = LeftMenu.getLayoutParams().width = ScreenWidth / 2;
ContentMenu.getLayoutParams().width = ScreenWidth;
once = true;
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
super.onLayout(changed, l, t, r, b);
if (changed) {
scrollTo(leftMenuWidth, 0);
}
}
boolean isOpen;
@Override
public boolean onTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
switch (ev.getAction()) {
case MotionEvent.ACTION_UP:
int scrollX = getScrollX();
if (scrollX >= leftMenuWidth / 2) {
scrollTo(leftMenuWidth, 0);
isOpen = false;
} else {
scrollTo(0, 0);
isOpen = true;
}
return true;
}
return super.onTouchEvent(ev);
}
public void open() {
if (isOpen) {
return;
}
scrollTo(0, 0);
isOpen = true;
}
public void close() {
if (isOpen) {
scrollTo(leftMenuWidth, 0);
isOpen = false;
}
}
public void toggle() {
if (isOpen) {
close();
} else {
open();
}
}
}
4、public class MainActivity extends Activity {
SldingMenu menu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
menu = (SldingMenu) findViewById(R.id.menu);
}
public void click(View v) {
menu.toggle();
}
}
搞定!!!!!!
- 最简单的slidingMenu
- 最简单的基于SlidingMenu仿QQ侧滑效果
- 最简单的侧滑slidingmenu+三个fragment
- SlidingMenu最简单的使用,实现侧拉效果
- SlidingMenu的简单使用
- SlidingMenu的简单写法
- slidingmenu的简单用法
- SlidingMenu的简单使用
- 开源项目SlidingMenu的简单使用
- android SlidingMenu sample的j简单写法
- Android SlidingMenu Fragment的简单优化
- 使用slidingMenu实现简单的侧滑栏
- SlidingMenu侧滑的简单实现
- android SlidingMenu侧滑的简单使用
- SlidingMenu侧滑的简单使用
- 简单的侧滑菜单SlidingMenu
- 安卓简单的SlidingMenu使用抽屉
- 通过SlidingMenu Fragment实现当前最流行的侧滑
- hdu1228A+B
- 设计模式,行为模式(观察者模式)
- Jenkins(二)linux slave构建
- php 异常Exception
- Oracle数据库学习(四)--高级查询
- 最简单的slidingMenu
- java jdbc线程池的使用
- linux常用命令
- TCP/IP网路协议
- Oracle数据库学习(五)--视图,序列,索引,约束
- 自己封装的JSONP跨域函数
- I/O Multiplexing & epoll
- LeetCode 142. Linked List Cycle II
- Android内存优化之OOM(转载自AndroidBus的LM航写的博客)