Android PopupWindow类

来源:互联网 发布:qt保存数据到文件 编辑:程序博客网 时间:2024/06/06 07:06

Android PopupWindow类

PopupWindow就是弹出窗口的意思。

1、主要方法

setWidth(int width) // 设置宽度setHeight(int height) // 设置高度setBackgroundDrawable(Drawable background) // 设置背景setContentView(View contentView) // 设置界面setTouchable(boolean touchable) // true时界面可点setFocusable(boolean focusable) // true时PopupWindow处理返回键setOutsideTouchable(boolean touchable) // true时点击外部消失,如果touchable为false时,点击界面也消失

2、布局文件activity_popup_window.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="match_parent">    <Button        android:id="@+id/btn_show_popup_window"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="show popup window"/>    <RadioGroup        android:id="@+id/rg_show"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <RadioButton            android:id="@+id/rb_show_at_location"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="showAtLocation"            android:checked="true"/>        <RadioButton            android:id="@+id/rb_show_as_drop_down"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="showAsDropDown"/>    </RadioGroup>    <CheckBox        android:id="@+id/cb_touchable"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="touchable"/>    <CheckBox        android:id="@+id/cb_focusable"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="focusable"/>    <CheckBox        android:id="@+id/cb_outside_touchable"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="outsideTouchable"/></LinearLayout>

3、显示不同的弹窗

在Activity中根据不同的选项,弹出不同的弹窗。
public class PopupWindowActivity extends Activity implements View.OnClickListener {private Button mBtnShowPopupWindow;private RadioGroup mRgShow;private RadioButton mRbShowAtLocation, mRbShowAsDropDown;private CheckBox mCbTouchable, mCbFocusable, mCbOutsideTouchable;private PopupWindow mPopupWindow;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_popup_window);mBtnShowPopupWindow = (Button) findViewById(R.id.btn_show_popup_window);mBtnShowPopupWindow.setOnClickListener(this);mRgShow = (RadioGroup) findViewById(R.id.rg_show);mRbShowAtLocation = (RadioButton) findViewById(R.id.rb_show_at_location);mRbShowAsDropDown = (RadioButton) findViewById(R.id.rb_show_as_drop_down);mCbTouchable = (CheckBox) findViewById(R.id.cb_touchable);mCbFocusable = (CheckBox) findViewById(R.id.cb_focusable);mCbOutsideTouchable = (CheckBox) findViewById(R.id.cb_outside_touchable);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btn_show_popup_window:showPopupWindow();break;case R.id.tv_take_photo:case R.id.tv_take_picture:case R.id.tv_cancel:mPopupWindow.dismiss();break;}}private void showPopupWindow() {if (mPopupWindow != null && mPopupWindow.isShowing()) {return;}mPopupWindow = new PopupWindow(this);// 设置宽度mPopupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);// 设置高度mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);// 设置背景mPopupWindow.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.product_bg)));View view = getLayoutInflater().inflate(R.layout.popup_window_picture, null, false);view.findViewById(R.id.tv_take_photo).setOnClickListener(this);view.findViewById(R.id.tv_take_picture).setOnClickListener(this);view.findViewById(R.id.tv_cancel).setOnClickListener(this);// 设置界面mPopupWindow.setContentView(view);// true时界面可点mPopupWindow.setTouchable(mCbTouchable.isChecked());// true时PopupWindow处理返回键mPopupWindow.setFocusable(mCbFocusable.isChecked());// true时点击外部消失,如果touchable为false时,点击界面也消失mPopupWindow.setOutsideTouchable(mCbOutsideTouchable.isChecked());// dismiss监听器mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {@Overridepublic void onDismiss() {mPopupWindow = null;}});if (mRgShow.getCheckedRadioButtonId() == R.id.rb_show_at_location) {mPopupWindow.showAtLocation(mBtnShowPopupWindow, Gravity.BOTTOM, 0, 0);} else if (mRgShow.getCheckedRadioButtonId() == R.id.rb_show_as_drop_down) {mPopupWindow.showAsDropDown(mBtnShowPopupWindow);}}}

4、显示

showAtLocation(View parent, int gravity, int x, int y) // 指定显示位置

显示如下


showAsDropDown(View anchor) // 显示在某个View的下面

显示如下


5、设置弹窗显示效果

setAnimationStyle(R.style.popupwindow_animation_style)
在styles.xml文件中
<style name="popupwindow_animation_style"><item name="android:windowEnterAnimation">@anim/anim_enter_from_bottom</item><item name="android:windowExitAnimation">@anim/anim_exit_to_bottom</item></style>
anim_enter_from_bottom.xml文件
<?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android"    android:fromYDelta="100%"    android:toYDelta="0"    android:duration="1000" />
anim_exit_to_bottom.xml文件
<?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android"    android:fromYDelta="0"    android:toYDelta="100%"    android:duration="1000" />

显示如下


6、设置背景透明度

设置背景透明度可以突出弹窗效果
private void backgroundAlpha(float bgAlpha) {WindowManager.LayoutParams lp = getWindow().getAttributes();lp.alpha = bgAlpha; //0.0-1.0getWindow().setAttributes(lp);getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);}
显示如下

0 0
原创粉丝点击