、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
该方法,,我写成了一个类,可以再任何工程中调用
首先,,,实现侧滑菜单,,需要加载一个jar包,,,,,
slidingmenulibary jar包
。。。。。。下面是这个类里面的数据
package com.example.utils;
import java.util.ArrayList;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.example.adapter.LeftMenuAdapter;
import com.example.login.LoginActivity;
import com.example.login.MoreLoginActivity;
import com.example.tadaynews.MainActivity;
import com.example.tadaynews.R;
import com.example.view.DownLoadActivity;
import com.example.view.Left_MenuSetActivity;
import com.example.vo.LeftMenuGood;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.lidroid.xutils.BitmapUtils;
import com.umeng.socialize.bean.SHARE_MEDIA;
/**
*
* 工具类
* 该类用于加载侧滑菜单中的布局控件
* 静态类
* @author 浅议爱
*
*/
public class SlidingMenuUtils {
private static ArrayList<LeftMenuGood> list_left;
private static LeftMenuAdapter adapter;
private static String left_image;//用户图片
private static String left_personname;//用户名
private static String message;//信息
private static WindowManager manager;
private static Button btn_night;
private final static String NIGHT = "night";
private static String DAY="day";
private static WindowManager mWindowManager;
private static MainActivity mainActivity;
private static View myView;
private static SharedPreferences spf;
private static int flage = 0;
// public SlidingMenuUtils(String left_image, String left_personname,String message) {
// super();
// this.left_image = left_image;
// this.left_personname = left_personname;
// this.message=message;
// }
public static void getMenu(final Context context,String left_personname,String left_image,String message){
mainActivity = (MainActivity) context;
mWindowManager = (WindowManager)mainActivity.getSystemService(Context.WINDOW_SERVICE);
init();
// TODO Auto-generated method stub
//实例化SlideMenu
SlidingMenu menu = new SlidingMenu(context);
//设置划出位置,左边
menu.setMode(SlidingMenu.LEFT);
View view=View.inflate(context, R.layout.left_item,null);
//为侧滑菜单设置布局
menu.setMenu(view);
//划出菜单的模式
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
menu.setShadowWidthRes(R.dimen.shadow_width);
menu.setShadowDrawable(R.drawable.shadow);
//设置滑动页面后,滑动的宽度(右边距为150dp),这里指的是可滑动的部分
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
//设置键入检出的变化程度
menu.setFadeDegree(0.2f);
//左菜单有title栏
getleftmenudata();
ListView left_lv=(ListView)view. findViewById(R.id.left_Menu_listview);
adapter=new LeftMenuAdapter(context,list_left);
left_lv.setAdapter(adapter);
//获取leftmenu上的图片按钮
//获取QQ登录的id
ImageView QQ_iamge=(ImageView)view. findViewById(R.id.qq_image);
//新浪
ImageView sinla=(ImageView)view. findViewById(R.id.weibo_image);
//更多登录方式
ImageView more_login=(ImageView) view.findViewById(R.id.more_login);
ImageView login_image=(ImageView) view.findViewById(R.id.login_image);
Button btn_set=(Button) view.findViewById(R.id.left_menu_set);
btn_night = (Button) view.findViewById(R.id.Left_menu_night);
Button btn_xiazai=(Button) view.findViewById(R.id.left_menu_xiazai);
menu.attachToActivity(mainActivity, SlidingMenu.SLIDING_CONTENT);
ImageView left_person_image=(ImageView) view.findViewById(R.id.left_menu_person);//用户登陆后的图片
TextView left_person_name=(TextView) view.findViewById(R.id.left_menu_name);//用户登陆后的图片
menu.showMenu();
//获取左上角的两个布局的id
RelativeLayout left_login=(RelativeLayout) view.findViewById(R.id.left_menu_login);
RelativeLayout left_person=(RelativeLayout) view.findViewById(R.id.left_menu_person_main);
if (message!=null) {
if (message.equals("success")) {
BitmapUtils utils=new BitmapUtils(context);
utils.display(left_person_image, left_image);//图片
left_person_name.setText(left_personname);
left_login.setVisibility(RelativeLayout.GONE);//登录界面隐藏
left_person.setVisibility(RelativeLayout.VISIBLE);//用户名页面显示
}else{
adapter=new LeftMenuAdapter(context,list_left);
left_lv.setAdapter(adapter);
}
}
// 夜间模式
btn_night.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flage%2==0){
night();
btn_night.setBackgroundResource(R.drawable.dayicon_leftdrawer_normal_night);
flage++;
}else{
day();
btn_night.setBackgroundResource(R.drawable.nighticon_leftdrawer_normal);
flage++;
}
}
});
//设置监听,QQ登录
QQ_iamge.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
UmengUtils.uMengLogin(context, SHARE_MEDIA.QQ);
}
});
//新浪微博
sinla.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
UmengUtils.uMengLogin(context, SHARE_MEDIA.SINA);
}
});
//更多登录
more_login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent it=new Intent(context,MoreLoginActivity.class);
context.startActivity(it);
}
});
//手机登陆
login_image.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent it=new Intent(context,LoginActivity.class);
context.startActivity(it);
}
});
//离线下载
btn_xiazai.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent it=new Intent(context,DownLoadActivity.class);
context.startActivity(it);
}
});
//设置
btn_set.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(context, Left_MenuSetActivity.class);
context.startActivity(intent);
}
});
}
private static void init() {
spf = mainActivity.getSharedPreferences("skinchange", Context.MODE_PRIVATE);
String mode = spf.getString("skin", "");
if(mode==null || !mode.equals("")){
if(mode.equals(NIGHT)){
night();
}else{
day();
}
}
}
private static void day() {
// TODO Auto-generated method stub
if(myView!=null){
mWindowManager.removeView(myView);
Editor edit = spf.edit();
edit.putString("skin", DAY);
edit.commit();
}
}
private static void night() {
// TODO Auto-generated method stub
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity=Gravity.BOTTOM;
params.y=10;
if(myView==null){
myView=new TextView(mainActivity);
myView.setBackgroundColor(0x80000000);
}
mWindowManager.addView(myView, params);
Editor edit = spf.edit();
edit.putString("skin", NIGHT);
edit.commit();
}
private static void getleftmenudata() {
list_left=new ArrayList<LeftMenuGood>();
int[] image={R.drawable.dynamicicon_leftdrawer,R.drawable.topicicon_leftdrawer,R.drawable.sellicon_leftdrawer,R.drawable.favoriteicon_leftdrawer,R.drawable.left_shop,R.drawable.feedbackicon_leftdrawer};
String[] name={"好友动态","我的话题","活动","收藏","商城","反馈"};
LeftMenuGood leftgood=null;
for (int i = 0; i < name.length; i++) {
leftgood=new LeftMenuGood(name[i],image[i]);
list_left.add(leftgood);
}
}
public void removeSkin(){
if(myView!=null){
mWindowManager.removeView(myView);
}
}
protected void onDestroy() {
String mode = spf.getString("skin", "");
if(mode.equals(NIGHT)){
removeSkin();
}
}
}
。、、、在Mainactivity调用 menu.totle方法就可以从侧面展示侧滑菜单、、、、、