资讯专栏INFORMATION COLUMN

PopupWindow 使用详解

huaixiaoz / 1779人阅读

摘要:在经常使用,效果跟效果类似,不同点在于可以控制显示的位置,比如底部显示等。至此,本篇已结束,如有不对的地方,欢迎您的建议与指正。同时期待您的关注,感谢您的阅读,谢谢

极力推荐文章:欢迎收藏
Android 干货分享

阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android

本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以下内容:

PopupWindow 继承关系

PopupWindow 使用方法

PopupWindow 底部PopupWindow的实现

PopupWindow 是一个可以在Activity 之上显示任意View的控件。在Android经常使用,效果跟Dialog 效果类似,不同点在于可以控制显示的位置,比如底部显示等。

1. PopupWindow简介 PopupWindow继承关系如下:
java.lang.Object
   ↳
     android.widget.PopupWindow
2. 使用方法

主要是调用PopWindow的构造方法,通过LayoutInflaterLayout转换成View,然后将View 传递过去,既可以实现,具体可以参考PopupWindow 源码,源码路径如下:framework/base/core/java/android/widget/PopupWindow.java

3. 底部PopupWindow的实现

PopupWindow实现效果

PopWindow 实现类

public class PopWindowMethods extends Activity {
    private View mPopView;
    private PopupWindow mPopupWindow;
    private Button btn_pop_ok;
    private Button btn_pop_cancel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_popwindow);

        InitPopWindow();
        InitView();
        InitClick();
    }

    /**
     * 
     */
    private void InitClick() {
        // TODO Auto-generated method stub
        btn_pop_ok.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "ok", 0).show();
            }
        });

        btn_pop_cancel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "cancel", 0).show();
            }
        });
    }

    /**
     * 
     */
    private void InitPopWindow() {
        // TODO Auto-generated method stub
        // 将布局文件转换成View对象,popupview 内容视图
        mPopView = getLayoutInflater().inflate(R.layout.popwindow_layout, null);
        // 将转换的View放置到 新建一个popuwindow对象中
        mPopupWindow = new PopupWindow(mPopView,
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        // 点击popuwindow外让其消失
        mPopupWindow.setOutsideTouchable(true);
        // mpopupWindow.setBackgroundDrawable(background);

    }

    /**
     * 
     */
    private void InitView() {
        btn_pop_ok = (Button) mPopView.findViewById(R.id.btn_pop_ok);
        btn_pop_cancel = (Button) mPopView.findViewById(R.id.btn_pop_cancel);

    }

    @SuppressLint("NewApi")
    public void ShowPopWindow(View view) {
        if (mPopupWindow.isShowing()) {
            mPopupWindow.dismiss();
        } else {
            // 设置PopupWindow 显示的形式 底部或者下拉等
            // 在某个位置显示
            mPopupWindow.showAtLocation(mPopView, Gravity.BOTTOM, 0, 30);
            // 作为下拉视图显示
            // mPopupWindow.showAsDropDown(mPopView, Gravity.CENTER, 200, 300);
        }

        // Toast.makeText(
        // getApplicationContext(),
        // "Launcher:"
        // + PackageUtils.isLauncherAPK(getApplicationContext(),
        // "com.miui.home"),
        // 0).show();
    }
}

PopupWindow布局




    

        

        

        

        

            

至此、PopWindow 的使用方法基本结束。

至此,本篇已结束,如有不对的地方,欢迎您的建议与指正。同时期待您的关注,感谢您的阅读,谢谢!

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/75843.html

相关文章

发表评论

0条评论

最新活动
阅读需要支付1元查看
<