PopupWindow完成对话框

1)在需要设置成对话框的Activity在AndroidManifest.xml中配置
android:theme=”@android:style/Theme.Dialog”
2)PopWindow也可以做类似对话框风格的窗口
只需要两步就可以完成:
a)创建PopWindow对象,为其设置布局内容与宽度、高度
b)调用pop.showAsDropDown(View)将PopupWindow作为View组件以下拉组件显示出来, 或者调用showAtLocation()方法将PopupWindow在指定位置显示出来
第一个参数指定PopupWindow的锚点view,即依附在哪个view上。
第二个参数指定起始点。
第三,四个参数设置以起始点的右下角为原点,向向右、下各偏移量。

package com.xspacing.popwindow;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;

/**
 * @ClassName MainActivity.java
 * @Description TODO
 * @author smile
 * @version v1.0
 * @date 2016年9月28日
 */
public class MainActivity extends Activity {

    public PopupWindow mPopWindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void showPopWindow(View v) {
        getPopWindow();
        mPopWindow.showAsDropDown(v);
        // 其他显示PopWindow位置的方法
        // mPopWindow.showAsDropDown(anchor, xoff, yoff);
        // mPopWindow.showAsDropDown(anchor, xoff, yoff, gravity);
        // mPopWindow.showAtLocation(v, Gravity.CENTER, 200, 200);
    }

    private void getPopWindow() {
        if (mPopWindow != null) {
            mPopWindow.dismiss(); // 关闭PopWindow
            return;
        } else {
            initPopWindow();
        }
    }

    @SuppressLint("ClickableViewAccessibility")
    private void initPopWindow() {
        View contentView = View.inflate(this, R.layout.pop_contentview, null);
        // 生成PopWindow
        mPopWindow = new PopupWindow(contentView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, true);
        contentView.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (mPopWindow != null && mPopWindow.isShowing()) {
                    mPopWindow.dismiss();
                    mPopWindow = null;
                }
                return false; // 返回值为true,表示消耗了本次事件,事件不会往后传递,false则反之
            }
        });
    }

}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.xspacing.popwindow.MainActivity" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:onClick="showPopWindow"
        android:text="显示PopWindow" />

</RelativeLayout>

pop_contentview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:text="分享微信" />

    <Button
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:text="分享QQ" />

       <Button
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:text="分享新浪微博" />
          <Button
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:text="分享朋友圈" />

</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值