PopupWindow
中央位置的PopupWindow
將自定義PopupWindow布局顯示在Activity中央位置,并且屏蔽PopupWindow之外的其他區(qū)域。
xml布局
布局中額外加上LinearLayout,主要是因?yàn)镻opupWindow會將最外層的Layout失效,會影響到中央位置的設(shè)置
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#66000000">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_centerVertical="true"
android:orientation="horizontal"
android:padding="10dp">
<Button
android:id="@+id/btn_edit"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/colorDarkCyan"
android:gravity="center"
android:text="@string/text_eidt"
android:textAllCaps="false"
android:textColor="@color/colorWhiteSmoke"
android:textSize="16sp" />
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="@color/colorWhiteSmoke" />
<Button
android:id="@+id/btn_cancel"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/colorDarkCyan"
android:gravity="center"
android:text="@string/text_cancel"
android:textAllCaps="false"
android:textColor="@color/colorWhiteSmoke"
android:textSize="16sp" />
</LinearLayout>
</RelativeLayout>
自定義動畫樣式
創(chuàng)建動畫資源目錄res/anim,并在此目錄創(chuàng)建兩個(gè)資源文件:context_menu_enter.xml / context_menu_exit.xml
然后依據(jù)此兩個(gè)動畫創(chuàng)建一個(gè)動畫樣式contextMenuAnim,用于PopupWindow
res/anim/context_menu_enter.xml (進(jìn)入動畫效果)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_shortAnimTime"
android:fromXDelta="0"
android:fromYDelta="100%p"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:toXDelta="0"
android:toYDelta="0"/>
</set>
res/anim/context_menu_exit.xml (退出動畫效果)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_shortAnimTime"
android:fromXDelta="0"
android:fromYDelta="0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:toXDelta="0"
android:toYDelta="100%p"/>
</set>
res/values/styles.xml (動畫樣式)
<style name="contextMenuAnim" parent="@android:style/Animation.Activity">
<item name="android:windowEnterAnimation">@anim/context_menu_enter</item>
<item name="android:windowExitAnimation">@anim/context_menu_exit</item>
</style>
關(guān)于translate移動方向和位置的示意圖:

anim_style.png
示例代碼
@SuppressLint("InflateParams")
private void showCenterPopupWindow() {
View view = getLayoutInflater().inflate(R.layout.layout_popupwindow_center, null);
Button mEditBtn = (Button) view.findViewById(R.id.btn_edit);
Button mCancelBtn = (Button) view.findViewById(R.id.btn_cancel);
mEditBtn.setOnClickListener(this);
mCancelBtn.setOnClickListener(this);
mPopupWindow = new PopupWindow(view); // 點(diǎn)擊外部區(qū)域,彈窗不消失
// 為PopupWindow菜單添加陰影時(shí),其寬度和高度必須都設(shè)置為MATCH_PARENT
mPopupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
mPopupWindow.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
ColorDrawable mColorDrawable = new ColorDrawable(0);
mPopupWindow.setBackgroundDrawable(mColorDrawable);
mPopupWindow.setAnimationStyle(R.style.contextMenuAnim); // 自定義動畫樣式
// mPopupWindow.setAnimationStyle(android.R.style.Animation_Translucent);
mPopupWindow.getBackground().setAlpha(100);
mPopupWindow.setOutsideTouchable(true); // 點(diǎn)擊PopupWindow以外區(qū)域,自動消失,這里PopupWindow為MATCH_PARENT,故無效
mPopupWindow.setFocusable(true); // 如果PopupWindow中有Editor的話,focusable要為true
mPopupWindow.setTouchable(true); //
mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
View rootView = LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_main, null);
mPopupWindow.showAtLocation(rootView, Gravity.CENTER, 0, 0);
}
示例效果

popupwindow_center.gif
底部位置的PopupWindow
xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:padding="16dp">
<Button
android:id="@+id/btn_open"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#7aab43"
android:textAllCaps="false"
android:textColor="#FFFFFF"
android:textSize="16sp"
android:text="@string/text_open" />
<Button
android:id="@+id/btn_save_as"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginTop="8dp"
android:background="#d94d4d"
android:textAllCaps="false"
android:textColor="#FFFFFF"
android:textSize="16sp"
android:text="@string/text_save_as" />
<Button
android:id="@+id/btn_close"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginTop="8dp"
android:background="#2464ca"
android:textAllCaps="false"
android:textColor="#FFFFFF"
android:textSize="16sp"
android:text="@string/text_close" />
</LinearLayout>
示例代碼
@SuppressLint("InflateParams")
private void showBottomPopupWindow() {
setBackgroundAlpha(0.5f); // 設(shè)置背景透明度
View view = getLayoutInflater().inflate(R.layout.layout_popupwindow_bottom, null);
Button mOpenBtn = (Button) view.findViewById(R.id.btn_open);
Button mSaveAsBtn = (Button) view.findViewById(R.id.btn_save_as);
Button mCloseBtn = (Button) view.findViewById(R.id.btn_close);
mOpenBtn.setOnClickListener(this);
mSaveAsBtn.setOnClickListener(this);
mCloseBtn.setOnClickListener(this);
// PopupWindow會丟棄掉最外層的父局部,故第二個(gè)LinearLayout才會起作用
mPopupWindow = new PopupWindow(view);
mPopupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
// 背景
ColorDrawable cd = new ColorDrawable(0);
mPopupWindow.setBackgroundDrawable(cd);
mPopupWindow.setAnimationStyle(android.R.style.Animation_Translucent);
View rootView = LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_main, null);
mPopupWindow.showAtLocation(rootView, Gravity.BOTTOM, 0, 0); // showAtLocation()顯示在指定位置
mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
setBackgroundAlpha(1.0f); // 恢復(fù)背景透明度
}
});
}
// 設(shè)置屏幕背景透明度
private void setBackgroundAlpha(float alpha){
WindowManager.LayoutParams mLayoutParams = getWindow().getAttributes();
mLayoutParams.alpha = alpha;
getWindow().setAttributes(mLayoutParams);
}
示例效果

popupwindow_bottom.gif
參照View位置的PopupWindow
xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp">
<Button
android:id="@+id/btn_share"
style="?android:attr/buttonStyleSmall"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:background="#b6c93c"
android:textColor="#FFFFFF"
android:textAllCaps="false"
android:text="@string/text_share" />
<Button
android:id="@+id/btn_collect"
style="?android:attr/buttonStyleSmall"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="#3cc998"
android:textColor="#FFFFFF"
android:textAllCaps="false"
android:text="@string/text_collect" />
<Button
android:id="@+id/btn_exit"
style="?android:attr/buttonStyleSmall"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="#3ca8c9"
android:textColor="#FFFFFF"
android:textAllCaps="false"
android:text="@string/text_exit" />
</LinearLayout>
</LinearLayout>
示例代碼
@SuppressLint("InflateParams")
private void showPositionPopupWindow() {
View view = getLayoutInflater().inflate(R.layout.layout_popupwindow_position, null);
Button mShareBtn = (Button) view.findViewById(R.id.btn_share);
Button mCollectBtn = (Button) view.findViewById(R.id.btn_collect);
Button mExitBtn = (Button) view.findViewById(R.id.btn_exit);
mShareBtn.setOnClickListener(this);
mCollectBtn.setOnClickListener(this);
mExitBtn.setOnClickListener(this);
mPopupWindow = new PopupWindow(view);
mPopupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
ColorDrawable cd = new ColorDrawable(0);
mPopupWindow.setBackgroundDrawable(cd);
mPopupWindow.setAnimationStyle(android.R.style.Animation_Translucent);
mPopupWindow.showAsDropDown(mPositionPopupWindowBtn); // showAsDropDown()顯示在一個(gè)參照視圖View的周圍
}
示例效果

popupwindow_position.gif