PopWindow使用方法详解

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

  1. 继承关系
  2. 使用方法
  3. 底部Popwindow的实现

1. 继承关系

PopWindow 继承关系如下:

java.lang.Object
   ↳
     android.widget.PopupWindow

2. 使用方法

主要是调用PopWindow的构造方法,通过LayoutInflater 将Layout转换成View,然后将View 传递过去,既可以实现,具体可以参考PopWindow 源码

PopWindows构造方法及常用方法

PopWindows构造方法及常用方法

3. 底部Popwindow的实现

    1. PopWindow 实现效果

底部Popwindow实现效果

底部Popwindow实现效果

    1. PopWindow 实现类
package com.programandroid.PopWindow;

import com.programandroid.R;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Toast;

/*
 * PopWindowMethods.java
 *
 *  Created on: 2017-11-1
 *      Author: wangjie
 * 
 *  Welcome attention to weixin public number get more info
 *
 *  WeiXin Public Number : ProgramAndroid
 *  微信公众号 :程序员Android
 *
 */
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);
        }

    }

}

    1. Popwindow 布局
<?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="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginTop="16dp"
    android:background="@drawable/popwindow_bg" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tv_pop_tittle"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:gravity="center"
            android:text="PopWindow Tittle" />

        <TextView
            android:id="@+id/tv_pop_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="left|center_horizontal"
            android:padding="20dp"
            android:text="是否要退出Popwindows ? " />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_margin="3dp"
            android:background="@color/white_line" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/btn_pop_ok"
                android:layout_width="0dp"
                android:layout_height="45dp"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:background="@drawable/custom_btn_white_selector"
                android:gravity="center"
                android:text="OK" />

            <Button
                android:id="@+id/btn_pop_cancel"
                android:layout_width="0dp"
                android:layout_height="45dp"
                android:layout_margin="10dp"
                android:layout_marginBottom="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:background="@drawable/custom_btn_white_selector"
                android:gravity="center"
                android:text="Cancel" />
        </LinearLayout>
    </LinearLayout>

</RelativeLayout>

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

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

如有侵权,请联系小编,小编对此深感抱歉,届时小编会删除文章,立即停止侵权行为,请您多多包涵。

既然都看到这里,领两个红包在走吧!
以下两个红包每天都可以领取

1.支付宝搜索 522398497,或扫码支付宝红包海报。

支付宝扫一扫,每天领取大红包

2.微信红包,微信扫一扫即可领取红包

 

微信扫一扫,每天领取微信红包

小礼物走一走,来简书关注我

posted @ 2017-11-02 09:53  程序员Android的博客  阅读(288)  评论(0编辑  收藏  举报