popup:弹出,window:界面。
PopupWindow:用来在Activity上弹出界面。
使用场景是,要在当前Activity上弹出其他界面的时候,需要用到它。
接下来认识下PopupWindow,
PopupWindow的构造函数为
public PopupWindow(View contentView, int width, int height, boolean focusable)
contentView为要显示的view,width和height为宽和高,值为像素值,也可以是MATCHT_PARENT和WRAP_CONTENT。
focusable为是否可以获得焦点,这是一个很重要的参数,也可以通过
public void setFocusable(boolean focusable)
来设置,如果focusable为false,在一个Activity弹出一个PopupWindow,按返回键,由于PopupWindow没有焦点,会直接退出Activity。如果focusable为true,PopupWindow弹出后,所有的触屏和物理按键都有PopupWindows处理。
如果PopupWindow中有Editor的话,focusable要为true。
第一次实现的时候遇到了问题,就是弹出框不会在按下Back键的时候消失,点击弹框外区域也没有正常消失,搜索了一下,都说只要设置背景就好了。然后我就找了个图片,果然弹框能正常dismiss了。
public class CustomSpinnerActivity extends Activity {
private ImageView spinner_img; // 按钮图片
private PopupWindow popupWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner);
View popupView = getLayoutInflater().inflate(R.layout.activity_popupwindow_1, null);
popupWindow = new PopupWindow(popupView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, true);
popupWindow.setTouchable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable(this.getResources(),""));
spinner_img = (ImageView) findViewById(R.id.spinner_arr_img);
spinner_img.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// showPopuWindows();
popupWindow.showAsDropDown(v);
}});
}
protected void showPopuWindows() {
// TODO Auto-generated method stub
// PopupWindow popupWindow = new PopupWindow(this, width, height, focusable)
}
}
activity_spinner.xml如下:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="20dp"
android:paddingTop="20dp"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/frame_color"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:id="@+id/spinner_content_et"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:background="@null"/>
<ImageView
android:id="@+id/spinner_arr_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_gravity="center_vertical"
android:background="@drawable/down_arrow_2"/>
</LinearLayout>
</RelativeLayout>activity_popupwindow_1.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000" >
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:text="@string/app_name"
android:textColor="#ffffffff"
android:layout_centerInParent="true"
android:gravity="center"/>
</RelativeLayout>
本文详细介绍了如何使用Android中的PopupWindow组件。重点讲解了构造函数的参数及其作用,并通过实例演示了如何创建并显示一个带有焦点功能的弹出窗口。同时,文章还提供了完整的代码示例。
17万+

被折叠的 条评论
为什么被折叠?



