参考了网上童鞋的作法,仔细研究了Popup Window 的用法,在此和大家分享下。
public class MainActivity extends Activity {
private Button bt_order;
private PopupWindow popupWindow;
private LinearLayout layout;
private ListView listView;
private String title[] = { "全部", "我的微博", "周边", "推荐","智能排版","广场", "同学","密友" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
bt_order = (Button) findViewById(R.id.tv_title);
bt_order.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showPopupWindowA();
// showPopupWindowB();
}
});
}
/**
* 使用 showAsDropDown 方法显示
*/
private void showPopupWindowB() {
layout = (LinearLayout) LayoutInflater.from(MainActivity.this).inflate(
R.layout.dialog, null);
listView = (ListView) layout.findViewById(R.id.lv_dialog);
listView.setAdapter(new ArrayAdapter<String>(MainActivity.this,
R.layout.text, R.id.tv_text, title));
popupWindow = new PopupWindow(MainActivity.this);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow
.setWidth(getResources().getDimensionPixelSize(
R.dimen.mark_popupwindow_width));
popupWindow.setHeight(getResources().getDimensionPixelSize(
R.dimen.mark_popupwindow_height));
popupWindow.setFocusable(true); // 设置PopupWindow可获得焦点
popupWindow.setTouchable(true); // 设置PopupWindow可触摸
popupWindow.setOutsideTouchable(false); // 设置非PopupWindow区域可触摸
popupWindow.setAnimationStyle(R.style.PopupAnimation);
popupWindow.setContentView(layout);
// popupWindow.showAsDropDown(bt_order);//popup window 位于锚点view左下角
// popupWindow.showAsDropDown(bt_order, 0, 0); //相对于锚点view bottom-left 的x,y offset
popupWindow.showAsDropDown(bt_order, (getResources().getDimensionPixelSize(
R.dimen.bt_width) - getResources().getDimensionPixelSize(
R.dimen.mark_popupwindow_width))/2, 0);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
bt_order.setText(title[arg2]);
popupWindow.dismiss();
popupWindow = null;
}
});
}
/**
* 使用 showAtLocation 方法显示,copy网上某位博主的实现方式
*/
public void showPopupWindowA() {
Rect frame = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int state_heght = frame.top;// 状态栏的高度
int y = bt_order.getBottom()+state_heght ;
int x = getWindowManager().getDefaultDisplay().getWidth()/2 - getResources().getDimensionPixelSize(
R.dimen.mark_popupwindow_width)/2;
layout = (LinearLayout) LayoutInflater.from(MainActivity.this).inflate(
R.layout.dialog, null);
listView = (ListView) layout.findViewById(R.id.lv_dialog);
listView.setAdapter(new ArrayAdapter<String>(MainActivity.this,
R.layout.text, R.id.tv_text, title));
popupWindow = new PopupWindow(MainActivity.this);
popupWindow.setBackgroundDrawable(new BitmapDrawable()); //设置空drawable作背景
popupWindow.setWidth(getResources().getDimensionPixelSize(
R.dimen.mark_popupwindow_width));
popupWindow.setHeight(getResources().getDimensionPixelSize(
R.dimen.mark_popupwindow_height));
popupWindow.setFocusable(true); // 设置PopupWindow可获得焦点
popupWindow.setTouchable(true); // 设置PopupWindow可触摸
popupWindow.setOutsideTouchable(false); // 设置非PopupWindow区域可触摸
popupWindow.setAnimationStyle(R.style.PopupAnimation);
popupWindow.setContentView(layout);
// popupWindow.showAtLocation(findViewById(R.id.main), Gravity.NO_GRAVITY, 0, 0);//需要指定Gravity,NO_GRAVITY等同于Left|Top
//x,y offset 是相对于Gravity的位置,当x,y为0 时,就完全是Gravity的位置
//showAtLocation()是在屏幕上的位置,showAsDropDown()是相对于锚点view的位置
popupWindow.showAtLocation(findViewById(R.id.main), Gravity.NO_GRAVITY, x, y);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
bt_order.setText(title[arg2]);
popupWindow.dismiss();
popupWindow = null;
}
});
}
}
上面两种方法,一种是使用showAtLocation(), 这种方式popupWindow的位置参考整个屏幕, showAsDropDown(),这种方式popupWindow的位置参考指定的view
popupAnimation.xml , R.style. popupAnimation
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="PopupAnimation" parent="android:Animation" mce_bogus="1">
<item name="android:windowEnterAnimation">@anim/popup_enter</item>
<item name="android:windowExitAnimation">@anim/popup_exit</item>
</style>
</resources>
R.anim.popup_enter
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale android:fromXScale="0.6" android:toXScale="1.0"
android:fromYScale="0.6" android:toYScale="1.0" android:pivotX="50%"
android:pivotY="50%" android:duration="1000" />
<alpha android:interpolator="@android:anim/decelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="2000" />
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale android:fromXScale="1.0" android:toXScale="0.5"
android:fromYScale="1.0" android:toYScale="0.5" android:pivotX="50%"
android:pivotY="50%" android:duration="500" />
<alpha android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="500" />
</set>
在github上有哥们将popupWindow封装了一下,可自定义popupWindow中的内容。
GitHub 地址 https://github.com/lorensiuswlt/NewQuickAction
可根据需要自定义popupWindow,或者使用GitHub上那哥们写好的框架。
工程下载地址 http://download.youkuaiyun.com/detail/dtnqy/7112529
为popupWindow设置动画
在res/anim添加2个文件
menu_bottombar_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="250"
android:fromYDelta="100.0%"
android:toYDelta="0.0" />
</set>
menu_bottombar_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="250"
android:fromYDelta="0.0"
android:toYDelta="100%" />
</set>
在res/value/styles.xml添加一个sytle
<style name="anim_menu_bottombar"> <item name="android:windowEnterAnimation">@anim/menu_bottombar_in</item> <item name="android:windowExitAnimation">@anim/menu_bottombar_out</item> </style>
pop.setAnimationStyle(R.style.anim_menu_bottombar);
pop.showAtLocation(text, Gravity.BOTTOM, 0, 0);
setAnimationStyle()要在show之前。