PopupWindow

应用展示

请添加图片描述
在这里插入图片描述

一.PopupWindow与Dialog区别

***Dialog显示的位置比较固定在屏幕中央,而PopupWindow则更加灵活***

二.PopupWindow的创建

 public PopupWindow(Context context) {
        this(context, null);
    }
 public PopupWindow(Context context, AttributeSet attrs) {
        this(context, attrs, com.android.internal.R.attr.popupWindowStyle);
    }

三.PopupWindow的显示

//指定父容器
public void showAtLocation(View parent, int gravity, int x, int y) {}
//指定控件
public void showAsDropDown(View anchor, int xoff, int yoff) {}

四.常用方法

setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
setContentView(sView);
dismiss();

四.PopupWindow添加阴影

将PopupWindow整屏显示,背景设置为浅灰色,将内容部分背景设置为白色。(看PopupWindow布局的背景色)

五.PopupWindow添加动画

setAnimationStyle(R.style.popupwindow_style);

六.完整代码

核心部分

public void showPopupWindow_down(){
        //R.layout.popupwindow_layout_buttom为PopupWindow布局
        View sView = LayoutInflater.from(this).inflate(R.layout.popupwindow_layout_buttom,null);
        final PopupWindow popupWindow = new PopupWindow(this);

        popupWindow.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setContentView(sView);

        //R.layout.activity_popup_window为Activity布局
        View perent = LayoutInflater.from(this).inflate(R.layout.activity_popup_window,null);

        //透明度区间0-255,值越大越不透明
        sView.getBackground().setAlpha((int) 0);
        popupWindow.showAtLocation(perent, Gravity.BOTTOM,0,0);

        //R.id.txt_change是PopupWindow布局中的一个控件
        TextView textView = sView.findViewById(R.id.txt_change);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(ContextActivity.this, "点了换相册", Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
            }
        });
    }
    
public void showPopupWindow_up(View item){
        //R.layout.popupWindow_layout_up 为PopupWindow布局
        View view = LayoutInflater.from(this).inflate(R.layout.pupupwindow_layout_up,null,false);
        final PopupWindow popupWindow = new PopupWindow(this);
        popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setContentView(view);
        //设置动画
        popupWindow.setAnimationStyle(R.style.popupwindow_style);
        popupWindow.showAsDropDown((View) item);
        //R.id.txt_add 为popupWindow布局中的控件
        TextView add = view.findViewById(R.id.txt_add);
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
    }

PopupWindow布局一

在这里插入图片描述

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#E6E6E6">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="bottom"
        android:background="#ffffff">

        <TextView
            android:id="@+id/txt_change"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="更换相册封面"
            android:textSize="30sp"
            android:gravity="center" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#E6E6E6"
            android:layout_marginTop="10dp"></View>

        <TextView
            android:id="@+id/txt_cancel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="取消"
            android:textSize="30sp"
            android:gravity="center"
            android:layout_marginTop="10dp"/>
    </LinearLayout>
</LinearLayout>

PopupWindow布局二

在这里插入图片描述

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="#ffffff">

    <ImageView
        android:id="@+id/img_chart"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@mipmap/chat_fill1"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="30dp"/>

    <TextView
        android:id="@+id/txt_chart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发起群聊"
        android:layout_toRightOf="@id/img_chart"
        android:textSize="30sp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="30dp"
        android:layout_marginTop="30dp"/>

    <ImageView
        android:id="@+id/img_add"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@mipmap/case_fill1"
        android:layout_below="@+id/img_chart"
        android:layout_alignLeft="@id/img_chart"
        android:layout_marginTop="30dp" />

    <TextView
        android:id="@+id/txt_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="添加好友"
        android:textSize="30sp"
        android:layout_alignLeft="@id/txt_chart"
        android:layout_below="@id/txt_chart"
        android:layout_marginTop="30dp"
        android:layout_marginRight="30dp"/>

    <ImageView
        android:id="@+id/img_sao"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@mipmap/pic4"
        android:layout_below="@+id/img_add"
        android:layout_alignLeft="@id/img_add"
        android:layout_marginTop="30dp" />

    <TextView
        android:id="@+id/txt_sao"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="扫一扫"
        android:textSize="30sp"
        android:layout_alignLeft="@id/txt_add"
        android:layout_below="@id/txt_add"
        android:layout_marginTop="30dp"
        android:layout_marginRight="30dp"/>

    <ImageView
        android:id="@+id/img_fukuan"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@mipmap/case_fill1"
        android:layout_below="@+id/img_sao"
        android:layout_alignLeft="@id/img_sao"
        android:layout_marginTop="30dp"
        android:layout_marginBottom="30dp"/>

    <TextView
        android:id="@+id/txt_fukuan"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="收付款"
        android:textSize="30sp"
        android:layout_alignLeft="@id/txt_sao"
        android:layout_below="@id/txt_sao"
        android:layout_marginTop="30dp"
        android:layout_marginRight="30dp"
        android:layout_marginBottom="30dp"/>

动画

res/创建anim文件夹,然后再创建xml文件

popu_show.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromXDelta="0"
        android:toXDelta="0"
        android:fromYDelta="120"
        android:toYDelta="0"
        android:duration="500" />
    <alpha
        android:duration="1000"
        android:fromAlpha="0.0"
        android:fillAfter="true"
        android:toAlpha="1.0" >
    </alpha>
</set>
popu_hide.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromXDelta="0"
        android:toXDelta="0"
        android:fromYDelta="0"
        android:toYDelta="120"
        android:duration="500" />
</set>
编辑style.xml
	<style name="popupwindow_style">
        <item name="android:windowEnterAnimation">@anim/popu_show</item>
        <item name="android:windowExitAnimation">@anim/popu_hide</item>
	</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值