安卓开发--左侧或右侧出来的popuwindows替代drawerlayout侧拉菜单

来源:互联网 发布:遗传算法 船舶调度 编辑:程序博客网 时间:2024/05/18 00:14

开发中,由于项目需求,需要每一个fragment都有侧拉栏,第一时间想到的就是用drawerlayout,这是谷歌推荐的侧拉栏,用法非常简单,只要把drawerlayout作为布局的最外层,然后里面用两个大布局包着,一个是主布局,一个是侧拉菜单布局,然后再侧拉布局的父布局给个属性 :layout_gravity="left",这样用手势侧拉就能拉出来侧拉栏了,可以和toobar绑定,通过toggle 

//获取开关同时让开关和DrawerLayout关联在一起toggle = new ActionBarDrawerToggle(this, mDrawerLayout, 0, 0);getSupportActionBar().setDisplayHomeAsUpEnabled(true);//设置默认的标题不显示getSupportActionBar().setDisplayShowTitleEnabled(false);//设置点击事件,点击弹出menu界面mDrawerLayout.setDrawerListener(toggle);
不过正常四个fragment的话,底部导航按钮是不会被遮挡住的,这样拉出菜单底下按钮还可以点击切换,需求不允许,所以就考虑从简,用popuwindows代替侧拉栏

先看效果图:


其实大体逻辑上并不难,只是自定义popuwindos,然后动画样式设置是左到右,或者右到左,这种滑入滑出的感觉,然后监听dismiss,控制窗口透明度,就是周围变暗,突出菜单栏,点击事件可以构造方法传入包括更新popuwinds上的头像姓名也可以设置get方法拿到对象,

public TextView getStartview(){    return starttime;}

然后调用update()更新

popMenus2.getStartview().setText(pickstarttimet);popMenus2.update();popMenus2.showAtLocation(MainActivity.this.findViewById(R.id.main_layout),        Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);

接下来上代码:

1.布局

主布局就直接 relativlayout 替代标题(style是NoActionbar)

<RelativeLayout    android:layout_width="match_parent"    android:layout_height="45dp"    android:background="@color/titbar_color">    <ImageView        android:id="@+id/lefthaha"        android:clickable="true"        android:layout_marginLeft="20dp"        android:layout_centerVertical="true"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/title_but_left3x"/>    <TextView        android:id="@+id/titlehaha"        android:text="我是主题"        android:textSize="18sp"        android:textColor="@color/white"        android:layout_centerHorizontal="true"        android:layout_centerVertical="true"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <ImageView        android:id="@+id/righthaha"        android:clickable="true"        android:layout_marginRight="20dp"        android:layout_centerVertical="true"        android:layout_alignParentRight="true"        android:src="@drawable/title_but_right3x"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></RelativeLayout>
leftpopuwindows布局

不贴了就是普通从上到下的linerlayout

代码:

public class LeftPopupWindows extends PopupWindow {    private View mMenuView; // PopupWindow 菜单布局    private Context context; // 上下文参数    private OnClickListener myOnClick; // PopupWindow 菜单 空间单击事件    private LinearLayout shenqing;    private LinearLayout shenpi;    private LinearLayout gongxiangwj;    private LinearLayout exit;    private CircleImageView touxiang;    private TextView name;    private ImageView shenqing_red,shenpi_red,gongxiangwj_red;    public LeftPopupWindows(Activity context, OnClickListener myOnClick) {        super(context);        this.context = context;        this.myOnClick = myOnClick;        Init();    }    private void Init() {        // TODO Auto-generated method stub        // PopupWindow 导入        LayoutInflater inflater = (LayoutInflater) context                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);        mMenuView = inflater.inflate(R.layout.leftpopuwindows, null);        shenqing = (LinearLayout) mMenuView.findViewById(R.id.shenqing);        shenpi = (LinearLayout) mMenuView.findViewById(R.id.shenpi);        gongxiangwj = (LinearLayout) mMenuView.findViewById(R.id.gongxiangwj);        name = (TextView) mMenuView.findViewById(R.id.leftname);        touxiang  = (CircleImageView) mMenuView.findViewById(R.id.leftphoto);        shenpi_red = (ImageView) mMenuView.findViewById(R.id.shenpi_red);        shenqing_red = (ImageView) mMenuView.findViewById(R.id.shenqing_red);        gongxiangwj_red = (ImageView) mMenuView.findViewById(R.id.gongxiangwj_red);        exit = (LinearLayout) mMenuView.findViewById(R.id.exit);        touxiang.setOnClickListener(myOnClick);        //审批        shenpi.setOnClickListener(myOnClick);        //申请        shenqing.setOnClickListener(myOnClick);        //共享文件        gongxiangwj.setOnClickListener(myOnClick);        //退出        exit.setOnClickListener(myOnClick);        // 导入布局        this.setContentView(mMenuView);        // 设置动画效果        this.setAnimationStyle(R.style.AnimationLeftFade);        //防止虚拟键挡住        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);        //设置弹出窗体的 宽,高        this.setWidth(LayoutParams.WRAP_CONTENT);        this.setHeight(LayoutParams.MATCH_PARENT);        // 设置可触        this.setFocusable(true);        ColorDrawable dw = new ColorDrawable(0x0000000);        this.setBackgroundDrawable(dw);        // 单击弹出窗以外处 关闭弹出窗        mMenuView.setOnTouchListener(new OnTouchListener() {            @Override            public boolean onTouch(View v, MotionEvent event) {                // TODO Auto-generated method stub                int height = mMenuView.findViewById(R.id.pop_layout).getTop();                int y = (int) event.getY();                if (event.getAction() == MotionEvent.ACTION_UP) {                    if (y < height) {                        dismiss();                        setWindowAlpa(false);                    }                }                return true;            }        });    }    /**     * 动态设置Activity背景透明度     *     * @param isopen     */    public void setWindowAlpa(boolean isopen) {        if (Build.VERSION.SDK_INT < 11) {            return;        }        final Window window = ((Activity) context).getWindow();        final WindowManager.LayoutParams lp = window.getAttributes();        window.setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND, WindowManager.LayoutParams.FLAG_DIM_BEHIND);        ValueAnimator animator;        if (isopen) {            animator = ValueAnimator.ofFloat(1.0f, 0.5f);        } else {            animator = ValueAnimator.ofFloat(0.5f, 1.0f);        }        animator.setDuration(400);        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {            @TargetApi(Build.VERSION_CODES.HONEYCOMB)            @Override            public void onAnimationUpdate(ValueAnimator animation) {                float alpha = (float) animation.getAnimatedValue();                lp.alpha = alpha;                window.setAttributes(lp);            }        });        animator.start();    }

使用:

@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    lefticon = (ImageView) findViewById(R.id.lefthaha);    righticon = (ImageView) findViewById(R.id.righthaha);    title = (TextView) findViewById(R.id.titlehaha);    mainlayout = (LinearLayout) findViewById(R.id.mainlayout);    title.setText("你是电,你是光,你是唯一的智障");    lefticon.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View v) {            leftPopupWindows = new LeftPopupWindows(MainActivity.this,leftonclick );            leftPopupWindows.showAtLocation(mainlayout, Gravity.LEFT,0,0);            leftPopupWindows.setWindowAlpa(true);            leftPopupWindows.setOnDismissListener(new PopupWindow.OnDismissListener() {                @Override                public void onDismiss() {                    leftPopupWindows.setWindowAlpa(false);                }            });        }    });    righticon.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View v) {            rightpopuwindows = new RightPopupWindows(MainActivity.this,rightonclick );            rightpopuwindows.showAtLocation(mainlayout, Gravity.RIGHT,0,0);            rightpopuwindows.setWindowAlpa(true);            rightpopuwindows.setOnDismissListener(new PopupWindow.OnDismissListener() {                @Override                public void onDismiss() {                    rightpopuwindows.setWindowAlpa(false);                }            });        }    });}private View.OnClickListener leftonclick = new View.OnClickListener() {    @Override    public void onClick(View v) {        leftPopupWindows.dismiss();        switch (v.getId()) {            case R.id.leftphoto:                Toast.makeText(MainActivity.this, "头像", Toast.LENGTH_SHORT).show();                break;            case R.id.shenqing:                Toast.makeText(MainActivity.this, "菜单1", Toast.LENGTH_SHORT).show();                break;            case R.id.shenpi:                Toast.makeText(MainActivity.this, "菜单2", Toast.LENGTH_SHORT).show();                break;            case R.id.gongxiangwj:                Toast.makeText(MainActivity.this, "菜单3", Toast.LENGTH_SHORT).show();                break;            case R.id.exit:                Toast.makeText(MainActivity.this, "退出", Toast.LENGTH_SHORT).show();                break;        }    }};private View.OnClickListener rightonclick = new View.OnClickListener() {    @Override    public void onClick(View v) {        rightpopuwindows.dismiss();        switch (v.getId()) {            case R.id.leftphoto:                Toast.makeText(MainActivity.this, "头像", Toast.LENGTH_SHORT).show();                break;            case R.id.shenqing:                Toast.makeText(MainActivity.this, "菜单1", Toast.LENGTH_SHORT).show();                break;            case R.id.shenpi:                Toast.makeText(MainActivity.this, "菜单2", Toast.LENGTH_SHORT).show();                break;            case R.id.gongxiangwj:                Toast.makeText(MainActivity.this, "菜单3", Toast.LENGTH_SHORT).show();                break;            case R.id.exit:                Toast.makeText(MainActivity.this, "退出", Toast.LENGTH_SHORT).show();                break;        }    }};

demo下载地址 :

https://github.com/PangHaHa12138/LeftPopuwindowsDemo

感谢阅读  ~~have a nice day ~~