• [Android学习笔记]PopupWindow的使用


    什么时候使用PopupWindow?

    当业务需求的交互形式需要在当前页弹出一个简单可选项UI与用户进行交互时,可使用PopupWindow完成此功能开发

    Android Dev API Doc


    PopupWindow是一个View的容器,它不像Frament和Activity这些View容器一样有完整的生命周期。它只是用来简单呈现一个自定义View而已。

    使用PopupWindow的一般步骤:

    1.创建PopupWindow对象

    2.设置创建ContentView,并设置。创建ContentView的方法很多,Inflater动态加载也行,Java代码创建也行

    3.设置必要参数,Show PopWindow


     PopupWindow显示在屏幕上之后,它的显示是不会受到物理回退键影响的,要实现物理回退,需要添加额外代码

    主要方法:

    a).创建PopupWindow对象

    new PopupWindow(view ,LayoutParams,LayoutParams,focus);

    View为PopWindow承载的View

    LayoutParams为View的填充形式

    b).是否可以获得焦点

    setFocusable(bool)

    如果不为true,则PopWindow无法接受用户输入

    c).是否相应点击窗口外部事件

    setOutsideTouchable(bool)

    通过此属性配合添加一个BackgroundDrawable,则可实现点击窗口外部,弹窗消失的效果

    d).弹出PopupWindow

    showAtLocation()

    showAsDropDown() , 此方法会根据ParentView来决定Popup的位置,如果ParentView在屏幕顶端,Popup会出现在ParentView下端,反之会出现在ParentView上端

    e).设置PopupWindow对象的尺寸

    setWidth()

    setHeight()

    PopupWindow是ContentView的承载View,所以无论ContentView的尺寸是多少,PopupWindow的尺寸只由自身决定。

    所以如果忘记了setWidth,setHeight,那么调用showAsDropDown()的时候是看不到,因为PopupWindow对象本身尺寸为0


    例子:

    public class MainActivity extends Activity
    {
    
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            Button btnTop = (Button)findViewById(R.id.btn_topBtn);
            Button btnBottom = (Button)findViewById(R.id.btn_bottomBtn);
            
            btnTop.setOnClickListener(new btnClickListener());
            btnBottom.setOnClickListener(new btnClickListener());
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu)
        {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
        
        private void createPopup(View view)
        {
            PopupWindow pop = new PopupWindow();
            
            TextView textView = new TextView(this);
            textView.setText("i ' m textView");
            
            pop.setContentView(textView);
            pop.setBackgroundDrawable(new ColorDrawable(Color.RED));
            pop.setWidth(500);
            pop.setHeight(500);
            //pop.setFocusable(true);
            pop.setOutsideTouchable(true);
            pop.showAsDropDown(view);
        }
        
        private class btnClickListener implements OnClickListener
        {        
            @Override
            public void onClick(View arg0)
            {
                // TODO Auto-generated method stub
                createPopup(arg0);
            }    
        }
    }
    人生就是一局不能Again的DOTA
  • 相关阅读:
    Python+selenium怎么自定义函数进行翻页操作定位第二页元素
    序列
    元组
    读书笔记-活出意义来
    多线程源代码学习笔记
    MacOS环境下Redis安装
    django修改静态文件(css,js)之后,浏览器效果没改变
    JavaScript变量、值类型介绍和启发-day01
    99乘法法则(使用bootstrap做表格效果)
    do-while循环的与for循环,while循环的区别是什么——(摘抄)
  • 原文地址:https://www.cnblogs.com/hellenism/p/3602377.html
Copyright © 2020-2023  润新知