五分钟教你学会PopupWindow

作者: 洪生鹏 | 来源:发表于2016-06-28 21:36 被阅读1792次

    于自定义窗口,Android提供了PopupWindow,简单实用。
    下面我们来看看今天demo实现的效果:

    弹出PopupWindow

    PopupWindow的构造函数

    public PopupWindow(View contentView, int width, int height, boolean focusable)
    

    其中contentView为要显示的view,width和height为宽和高,
    值为像素值,可以是MATCHT_PARENT和WRAP_CONTENT
    来设置,如果focusable为false,在一个Activity弹出一个PopupWindow,按返回键,由于PopupWindow没有焦点,会直接退出Activity。如果focusable为true,PopupWindow弹出后,所有的触屏和物理按键都有PopupWindows处理。
    如果PopupWindow中有Editor的话,focusable要为true。

    下面来看一个简单的demo
    主界面:就简单放一个button,点击弹出我们需要的PopupWindow

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        xmlns:tools="http://schemas.android.com/tools"  
        android:id="@+id/container"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent" >  
        <Button  
            android:id="@+id/btnOpen"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_centerInParent="true"  
            android:text="@string/app_name" />  
    </RelativeLayout>  
    

    popupwindow 界面:放了2个button 和一个文本框,用来输入值

    <?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="#b5555555" >  
        <LinearLayout  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:layout_alignParentBottom="true"  
            android:background="#eee"  
            android:orientation="vertical" >  
            <EditText  
                android:id="@+id/leaveword"  
                android:layout_width="fill_parent"  
                android:layout_height="wrap_content"  
                android:layout_marginBottom="20dp"  
                android:layout_marginLeft="20dp"  
                android:layout_marginRight="20dp"  
                android:layout_marginTop="20dp"  
                android:gravity="top"  
                android:hint="说点什么吧~"  
                android:inputType="textMultiLine"  
                android:lineSpacingExtra="6.0dp"  
                android:maxHeight="150dp"  
                android:minHeight="100dp"  
                android:paddingLeft="10.0dp"  
                android:paddingRight="10.0dp"  
                android:paddingTop="10.0dp" />  
            <LinearLayout  
                android:layout_width="fill_parent"  
                android:layout_height="wrap_content"  
                android:layout_marginBottom="20dp"  
                android:gravity="center"  
                android:orientation="horizontal" >  
                <Button  
                    android:id="@+id/confirmButton"  
                    android:layout_width="80.0dip"  
                    android:layout_height="wrap_content"  
                    android:gravity="center"  
                    android:text="发表"  
                    android:textColor="#fff"  
                    android:textSize="16.0sp" />  
                <Button  
                    android:id="@+id/cancleButton"  
                    android:layout_width="80.0dip"  
                    android:layout_height="wrap_content"  
                    android:layout_marginLeft="30dp"  
                    android:gravity="center"  
                    android:text="取消"  
                    android:textColor="#565656"  
                    android:textSize="16.0sp" />  
            </LinearLayout>  
        </LinearLayout>  
    </RelativeLayout>  
    

    这样我们的界面布局就算完成了,下来我们来看一个Activity。写个了OpenView方法来弹出view

    public void OpenView() {  
        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);  
        popupWindowView = inflater.inflate(R.layout.popupwindow, null);  
        popupWindow = new PopupWindow(popupWindowView,  
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, true);  
        // 设置PopupWindow的弹出和消失效果  
        popupWindow.setAnimationStyle(R.style.popupAnimation);  
        btnsure = (Button) popupWindowView.findViewById(R.id.confirmButton);  
        btnsure.setOnClickListener(new ButtonOnClickListener());  
        cancleButton = (Button) popupWindowView.findViewById(R.id.cancleButton);  
        cancleButton.setOnClickListener(new ButtonOnClickListener());  
        leaveword = (EditText) popupWindowView.findViewById(R.id.leaveword);  
        popupWindow.showAtLocation(btnsure, Gravity.CENTER, 0, 0);  
    }  
    

    我们看到弹出来的有点动画效果,是因为我们在弹出时,加上了

    popupWindow.setAnimationStyle(R.style.popupAnimation); 
    

    我们需要在在styles.xml下加上popupAnimation

    <style name="popupAnimation" parent="android:Animation">  
       <item name="android:windowEnterAnimation">@anim/in</item>  
       <item name="android:windowExitAnimation">@anim/out</item>  
    lt;/style>  
    

    在工程res下新建anim文件夹,在anim文件夹先新建两个xml文件

    in.xml

    <?xml version="1.0" encoding="utf-8"?>  
    <set xmlns:android="http://schemas.android.com/apk/res/android" >  
        <translate  
            android:duration="1500"  
            android:fromYDelta="5000"  
            android:toYDelta="0" />  
    </set>  
    

    out.xml

    <?xml version="1.0" encoding="utf-8"?>  
    <set xmlns:android="http://schemas.android.com/apk/res/android">  
        <translate  
            android:fromYDelta="0"  
            android:toYDelta="5000"  
            android:duration="1500"  
        />  
    </set>  
    

    如果想要关闭弹出框
    调用popupWindow.dismiss();就好.
    完整的Activity源码

    public class MainActivity extends Activity {  
        private View popupWindowView;  
        private PopupWindow popupWindow;  
        private Button btnsure, cancleButton, btnOpen;  
        private EditText leaveword;  
      
        @Override  
        protected void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.activity_main);  
      
            btnOpen = (Button) findViewById(R.id.btnOpen);  
            btnOpen.setOnClickListener(new ButtonOnClickListener());  
        }  
      
        public void OpenView() {  
            LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);  
            popupWindowView = inflater.inflate(R.layout.popupwindow, null);  
            popupWindow = new PopupWindow(popupWindowView,  
                    LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, true);  
            // 设置PopupWindow的弹出和消失效果  
            popupWindow.setAnimationStyle(R.style.popupAnimation);  
            btnsure = (Button) popupWindowView.findViewById(R.id.confirmButton);  
            btnsure.setOnClickListener(new ButtonOnClickListener());  
            cancleButton = (Button) popupWindowView.findViewById(R.id.cancleButton);  
            cancleButton.setOnClickListener(new ButtonOnClickListener());  
            leaveword = (EditText) popupWindowView.findViewById(R.id.leaveword);  
            popupWindow.showAtLocation(btnsure, Gravity.CENTER, 0, 0);  
        }  
      
        private class ButtonOnClickListener implements OnClickListener {  
            @Override  
            public void onClick(View vid) {  
      
                switch (vid.getId()) {  
      
                case R.id.btnOpen:  
                    OpenView();  
                    break;  
                case R.id.confirmButton:  
      
                    Toast.makeText(MainActivity.this, leaveword.getText().toString(), Toast.LENGTH_SHORT)  
                            .show();  
      
                    break;  
                case R.id.cancleButton:  
                    popupWindow.dismiss();  
                    break;  
      
                default:  
                    break;  
                }  
            }  
        }  
      
    }  
    

    这样一个简单的PopupWindow例子就算完成了.

    相关文章

      网友评论

      • ChienYi:謝謝你
      • b6c7a3ded5ff:能设置从顶部滑动出来吗?
        洪生鹏:@dyhuang http://www.jianshu.com/p/802efdab0f4f
        洪生鹏:@dyhuang 可以的,改动anim里面的文件就好,稍后整理从不同方向滑动出来的
      • yzytmac:作者解决了我的问题,因为网上好多教程style都没有加parent,导致动画不起效,看到你的帖子发现了问题
        yzytmac: @爱开发 好的!
        洪生鹏:@雨小七 有帮助就好,感兴趣的话,关注一下我的微信公众号 aikaifa,不错过每一篇干货
      • thisfeng:重要的设置背景,点击窗外关闭没提到

      本文标题:五分钟教你学会PopupWindow

      本文链接:https://www.haomeiwen.com/subject/ciitjttx.html