您的位置:首页 > 移动开发 > 微信开发

android 仿微信底部弹出菜单

2013-07-14 20:20 411 查看
最近在网上看了一些文章介绍使用PopupWindow实现 微信弹出菜单,于是乎自己也实现了一下,分享一下!

原理:主要是 popupWindow.setAnimationStyle(R.style.popuStyle);//设置 popupWindow 动画样式

运行效果截图



package com.example.test;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

public class MainActivity extends Activity implements OnClickListener {
private PopupWindow popupWindow;
private Button bt_popup;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

findView();
}

private void findView() {
bt_popup = (Button) findViewById(R.id.bt_popup);
bt_popup.setOnClickListener(this);
}

private void showPopupWindow() {

View view = (LinearLayout) LayoutInflater.from(MainActivity.this)
.inflate(R.layout.popmenu, null);

Button bt_clear = (Button) view.findViewById(R.id.bt_clear);
Button bt_exit = (Button) view.findViewById(R.id.bt_exit);

bt_clear.setOnClickListener(this);
bt_exit.setOnClickListener(this);

if (popupWindow == null) {

popupWindow = new PopupWindow(MainActivity.this);
popupWindow.setBackgroundDrawable(new BitmapDrawable());

//			popupWindow.setFocusable(true); // 设置PopupWindow可获得焦点
popupWindow.setTouchable(true); // 设置PopupWindow可触摸
popupWindow.setOutsideTouchable(true); // 设置非PopupWindow区域可触摸

popupWindow.setContentView(view);

popupWindow.setWidth(LayoutParams.MATCH_PARENT);
popupWindow.setHeight(LayoutParams.WRAP_CONTENT);

popupWindow.setAnimationStyle(R.style.popuStyle);	//设置 popupWindow 动画样式
}

popupWindow.showAtLocation(bt_popup, Gravity.BOTTOM, 0, 0);

popupWindow.update();

}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_popup:
showPopupWindow();
break;
case R.id.bt_exit:
popupWindow.dismiss();
break;
case R.id.bt_clear:
popupWindow.dismiss();
break;
default:
break;
}
}
}


注意:popupWindow 的 showAtLocation(parent, gravity, x, y); 方法 第一个参数 parent 并不一定要求是 Activity 布局中的 根节点 元素,这里我传入了一个 Button

阅读官方文档中的说明 

parent a parent view to get the
android.view.View.getWindowToken()
token from

只是根据这个parent 获取 WindowToken 而已

popupWindow 动画样式

<style name="popuStyle">
<item name="android:windowEnterAnimation">@anim/popup_anim_in</item>
<item name="android:windowExitAnimation">@anim/popup_anim_out</item>
</style>


popup_anim_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<translate
android:duration="300"
android:fromXDelta="0"
android:fromYDelta="100%p"
android:toXDelta="0"
android:toYDelta="0" />

</set>


popup_anim_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<translate
android:duration="300"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="0"
android:toYDelta="100%p" />

</set>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息