android 右侧弹出layout,PopupWindow实现右侧、左侧和底部弹出菜单

先上图,4张:

22632e959c881a6be9122bd1cb58a988.png

ed37dc614ea89cc48e2a671e8a6a722c.png

06bf23a8d9cd864c2eb187b4d23e6838.png

c13553b3960b3d2f04f1c5fda10c40c9.png

项目代码:http://download..net/download/jianfengwen/9124745 (需要2个分)

项目SDK是5.1,建议将代码拷到自己的工程中去

代码如下:

MainActivity类:

package com.example.popupleftmenu;

import android.app.Activity;

import android.content.Context;

import android.graphics.drawable.ColorDrawable;

import android.os.Bundle;

import android.view.Gravity;

import android.view.MotionEvent;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.View.OnTouchListener;

import android.view.ViewGroup.LayoutParams;

import android.view.WindowManager;

import android.widget.Button;

import android.widget.PopupWindow;

import android.widget.Toast;

public class MainActivity extends Activity {

private Context context = null;

private PopupWindow popupWindow;

private int from = 0;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

context = this;

setContentView(R.layout.activity_main);

Button popLeftBtn = (Button)findViewById(R.id.pop_left_btn);

Button popRightBtn = (Button)findViewById(R.id.pop_right_btn);

Button popBottomBtn = (Button)findViewById(R.id.pop_bottom_btn);

popLeftBtn.setOnClickListener(popClick);

popRightBtn.setOnClickListener(popClick);

popBottomBtn.setOnClickListener(popClick);

}

OnClickListener popClick = new OnClickListener() {

@Override

public void onClick(View v) {

switch(v.getId()){

case R.id.pop_left_btn:{

from = Location.LEFT.ordinal();

break;

}

case R.id.pop_right_btn:{

from = Location.RIGHT.ordinal();

break;

}

case R.id.pop_bottom_btn:{

from = Location.BOTTOM.ordinal();

break;

}

}

//调用此方法,menu不会顶置

//popupWindow.showAsDropDown(v);

initPopupWindow();

}

};

/**

* 添加新笔记时弹出的popWin关闭的事件,主要是为了将背景透明度改回来

*

*/

class popupDismissListener implements PopupWindow.OnDismissListener{

@Override

public void onDismiss() {

backgroundAlpha(1f);

}

}

protected void initPopupWindow(){

View popupWindowView = getLayoutInflater().inflate(R.layout.pop, null);

//内容,高度,宽度

if(Location.BOTTOM.ordinal() == from){

popupWindow = new PopupWindow(popupWindowView, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, true);

}else{

popupWindow = new PopupWindow(popupWindowView, LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT, true);

}

//动画效果

if(Location.LEFT.ordinal() == from){

popupWindow.setAnimationStyle(R.style.AnimationLeftFade);

}else if(Location.RIGHT.ordinal() == from){

popupWindow.setAnimationStyle(R.style.AnimationRightFade);

}else if(Location.BOTTOM.ordinal() == from){

popupWindow.setAnimationStyle(R.style.AnimationBottomFade);

}

//菜单背景色

ColorDrawable dw = new ColorDrawable(0xffffffff);

popupWindow.setBackgroundDrawable(dw);

//宽度

//popupWindow.setWidth(LayoutParams.WRAP_CONTENT);

//高度

//popupWindow.setHeight(LayoutParams.FILL_PARENT);

//显示位置

if(Location.LEFT.ordinal() == from){

popupWindow.showAtLocation(getLayoutInflater().inflate(R.layout.activity_main, null), Gravity.LEFT, 0, 500);

}else if(Location.RIGHT.ordinal() == from){

popupWindow.showAtLocation(getLayoutInflater().inflate(R.layout.activity_main, null), Gravity.RIGHT, 0, 500);

}else if(Location.BOTTOM.ordinal() == from){

popupWindow.showAtLocation(getLayoutInflater().inflate(R.layout.activity_main, null), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);

}

//设置背景半透明

backgroundAlpha(0.5f);

//关闭事件

popupWindow.setOnDismissListener(new popupDismissListener());

popupWindowView.setOnTouchListener(new OnTouchListener() {

@Override

public boolean onTouch(View v, MotionEvent event) {

/*if( popupWindow!=null && popupWindow.isShowing()){

popupWindow.dismiss();

popupWindow=null;

}*/

// 这里如果返回true的话,touch事件将被拦截

// 拦截后 PopupWindow的onTouchEvent不被调用,这样点击外部区域无法dismiss

return false;

}

});

Button open = (Button)popupWindowView.findViewById(R.id.open);

Button save = (Button)popupWindowView.findViewById(R.id.save);

Button close = (Button)popupWindowView.findViewById(R.id.close);

open.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

Toast.makeText(context, "Open", Toast.LENGTH_LONG).show();

popupWindow.dismiss();

}

});

save.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

Toast.makeText(context, "Open", Toast.LENGTH_LONG).show();

popupWindow.dismiss();

}

});

close.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

Toast.makeText(context, "Open", Toast.LENGTH_LONG).show();

popupWindow.dismiss();

}

});

}

/**

* 设置添加屏幕的背景透明度

* @param bgAlpha

*/

public void backgroundAlpha(float bgAlpha)

{

WindowManager.LayoutParams lp = getWindow().getAttributes();

lp.alpha = bgAlpha; //0.0-1.0

getWindow().setAttributes(lp);

}

/**

* 菜单弹出方向

*

*/

public enum Location {

LEFT,

RIGHT,

TOP,

BOTTOM;

}

}

两个布局文件:

1.activity_main.xml,就三个Button

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical">

android:id="@+id/pop_left_btn"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/pop_left"/>

android:id="@+id/pop_right_btn"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/pop_right"/>

android:id="@+id/pop_bottom_btn"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/pop_bottom"/>

2. pop.xml,也是三个Button,可以自己修改

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/open"/>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/save"/>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/close"/>

strings.xml

弹出左侧菜单

弹出右侧菜单

弹出底部菜单

打开

保存

关闭

styles.xml

@anim/in_lefttoright

@anim/out_righttoleft

@anim/in_righttoleft

@anim/out_lefttoright

@anim/in_bottomtotop

@anim/out_toptobottom

左边弹出菜单动画文件:

in_lefttoright.xml:从左边入

android:fromXDelta="-100%"

android:toXDelta="0"

android:duration="500"/>

out_righttoleft.xml:从右边出

android:toXDelta="-100%"

android:duration="500"/>

其他动画文件自己参考写,就是fromXDelta, fromYDelta, toXDelta和toYDelta使用

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值