UI组件:PopupWindows的详细使用(一)

来源:互联网 发布:师洋淘宝骂人 编辑:程序博客网 时间:2024/06/06 18:03

一、定义

在官方文档里面,对PopupWindow做出了以下定义:

A popup window that can be used to display an arbitrary view. The popup window is a floating container that appears on top of the current activity.

意思就是这个popup window可以用于展示任意一个view,而且这个window会浮动于当前的activity之上。

二、构造器

PopupWindow(View contentView)Create a new non focusable popup window which can display the contentView.
PopupWindow(int width, int height)Create a new empty, non focusable popup window.
PopupWindow(View contentView, int width, int height)Create a new non focusable popup window which can display the contentView.
PopupWindow(View contentView, int width, int height, boolean focusable)Create a new popup window which can display the contentView.
由上述第三个构造器可知,在构造一个popup window的时候,需要传递一个view、该popup window的高度、宽度,这也是一个标准的popup window所需要的,但构造器一和二缺少了某些方法,这个可以通过它的方法传递相应的view或者高度、宽度。而构造器四,则多了一个focusable参数,代表是否获得焦点。


三、主要方法

1、Set方法:

    //为该popup window的显示设置一个动画效果    setAnimationStyle(int animationStyle)    //为该popup window设置一个背景    setBackgroundDrawable(Drawable background)    //设置popup window的内容    setContentView(View contentView)    //设置popup window的宽和高        setWidth(int width)/setHeight(int height)        //设置该popup window是否获得焦点    setFocusable(boolean focusable)    //设置popup window 之外的部分是否能点击    setOutsideTouchable(boolean touchable)

2、显示函数:

    //在anchor的左下方显示    showAsDropDown(View anchor)    //在anchor的左下方显示,x轴上偏移xoff,y轴上偏移yoff    showAsDropDown(View anchor, int xoff, int yoff)    //相对于父控件的显示位置,x、y为偏移量,gravity可选为Gravity.CENTER正中央,Gravity.BOTTOM下方    showAtLocation(View parent, int gravity, int x, int y)

四、简单的Popup Window的显示:

通过以下代码,将实现下图的PopupWindow的弹出:

                                         

(一)MainActivity的布局:布局很简单,只有一个按钮,以及改了一下背景颜色,这里不再给出

(二)PopupWindow的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#ffffff">    <View        android:layout_width="match_parent"        android:layout_height="2.25dp"        android:background="#fa7829"/>    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/pop_football"        android:text="足球"        android:gravity="center"        android:textSize="20sp"/>    <View        android:layout_width="match_parent"        android:layout_height="1dp"        android:background="#160f36"/>    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/pop_tennis"        android:text="网球"        android:gravity="center"        android:textSize="20sp"/>    <View        android:layout_width="match_parent"        android:layout_height="1dp"        android:background="#160f36"/>    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/pop_basketball"        android:text="篮球"        android:gravity="center"        android:textSize="20sp"/>    <View        android:layout_width="match_parent"        android:layout_height="1dp"/></LinearLayout>
(三)MainActivity.java:

public class MainActivity extends Activity implements View.OnClickListener {    private Button button;    PopupWindow mPopupWindow;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        button= (Button) findViewById(R.id.btn);        button.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                PopupWindowShow();            }        });    }    private void PopupWindowShow() {        View contentView= LayoutInflater.from(MainActivity.this).inflate(R.layout.contentview,null);        mPopupWindow=new PopupWindow(contentView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT,true);        TextView tv1= (TextView) contentView.findViewById(R.id.pop_football);        TextView tv2= (TextView) contentView.findViewById(R.id.pop_tennis);        TextView tv3= (TextView) contentView.findViewById(R.id.pop_basketball);        tv1.setOnClickListener(this);        tv2.setOnClickListener(this);        tv3.setOnClickListener(this);        View rootView=LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_main,null);        mPopupWindow.showAtLocation(rootView, Gravity.BOTTOM,0,0);    }    @Override    public void onClick(View v) {        int id=v.getId();        switch (id){            case R.id.pop_football:                Toast.makeText(this,"clicked football",Toast.LENGTH_SHORT).show();                mPopupWindow.dismiss();                break;            case R.id.pop_tennis:                Toast.makeText(this,"clicked tennis",Toast.LENGTH_SHORT).show();                mPopupWindow.dismiss();                break;            case R.id.pop_basketball:                Toast.makeText(this,"clicked basketball",Toast.LENGTH_SHORT).show();                mPopupWindow.dismiss();                break;        }    }}

对PopupWindowShow()函数做出如下解释:

①以下两行代码,首先获得popup window的布局文件,通过LayoutInflater获得,而LayoutInflater通过当前的context获得,

然后通过PopupWindow的构造器,传递contentView,以及相应的宽高,这里宽高选择wrap_content,最后一个true表示获得焦点。

View contentView= LayoutInflater.from(MainActivity.this).inflate(R.layout.contentview,null);mPopupWindow=new PopupWindow(contentView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT,true);
②首先,通过contentView获取在它内部的三个TextView,然后设置监听器。注意:如果没有写成contentView.findViewById,而直接使用findViewById,则会出错,因为这三个TextView是依附于contentView的,而不是父布局。

TextView tv1= (TextView) contentView.findViewById(R.id.pop_football);TextView tv2= (TextView) contentView.findViewById(R.id.pop_tennis);TextView tv3= (TextView) contentView.findViewById(R.id.pop_basketball);tv1.setOnClickListener(this);tv2.setOnClickListener(this);tv3.setOnClickListener(this);
③先获得父布局,即MainActivity的布局,然后利用showAtLocation显示在父布局的底部。

View rootView=LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_main,null);mPopupWindow.showAtLocation(rootView, Gravity.BOTTOM,0,0);






0 0
原创粉丝点击