Android基础之 popupwindow(弹框)

首先啊,创建popupwindow的时候呢,其实有很多选择可以使用。 这些都是官方给出的有参构造,我们可以根据对应的参数来实现自己想要的效果

    public PopupWindow(Context context) {
        throw new RuntimeException("Stub!");
    }

    public PopupWindow(Context context, AttributeSet attrs) {
        throw new RuntimeException("Stub!");
    }

    public PopupWindow(Context context, AttributeSet attrs, int defStyleAttr) {
        throw new RuntimeException("Stub!");
    }

    public PopupWindow(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        throw new RuntimeException("Stub!");
    }

    public PopupWindow() {
        throw new RuntimeException("Stub!");
    }

    public PopupWindow(View contentView) {
        throw new RuntimeException("Stub!");
    }

    public PopupWindow(int width, int height) {
        throw new RuntimeException("Stub!");
    }

    public PopupWindow(View contentView, int width, int height) {
        throw new RuntimeException("Stub!");
    }

    public PopupWindow(View contentView, int width, int height, boolean focusable) {
        throw new RuntimeException("Stub!");
    }

常用方法!

setContentView(View contentView);设置PopupWindow显示的View
showAsDropDown(View anchor);相对某个控件的位置(正左下方)无偏移
showAsDropDown(View anchor,int xoff,int yoff):相对于某个控件的位置有偏移
setFocusable(boolean focusable)设置是否获取焦点
setBackgroundDrawable(Drawable backround)设置背景
dismiss()关闭弹窗
setAnimationStyle(int animationStyle)设置加载动画
setTouchable(boolean touchable) 设置触摸本身
setOutideTouchable(boolean touchable)设置PopupWindow外面的触摸

上Demo

package com.example.mytoolbar;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Toast;

public class ToolbarActivity extends AppCompatActivity {

    private AlertDialog.Builder builder;

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


    public void kevin(View view) {
        View popup = getLayoutInflater().inflate(R.layout.popupwindow,null);
        Button btn_1 = popup.findViewById(R.id.btn_1);
        Button btn_2 = popup.findViewById(R.id.btn_2);
        /**
         * 第一个参数为自己创建的popupwindow的布局,第二、三个参数是宽和高,正常我们可以直接写死,但是我们为了
         * 让popupwindow刚好包裹住我们写的布局,我们使用ViewGroup.LayoutParams.WRAP_CONTENT
         * 第四个参数:外部是否可点击,如果为true 点击popupwindow外边就可以关闭popwindow
         */
        PopupWindow popupWindow = new PopupWindow(popup,
                ViewGroup.LayoutParams.WRAP_CONTENT ,
                ViewGroup.LayoutParams.WRAP_CONTENT,true);

        //设置popwindow背景图片 注意一定要在显示方法的前面添加
        popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_android_black_24dp));

        /**
         * showAsDropDown()相对于View显示的 如果参数只写一个View的话,那默认是出现在按钮的正下方
         * 其实我们可以这样写: popupWindow.showAsDropDown(view,view.getWidth(),-view.getHeight());  使用按钮的宽高来给定弹窗出现的位置
         *
         */
        popupWindow.showAsDropDown(view);
        //一般的话就是显示在最下方

        btn_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
            }
        });
        btn_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(ToolbarActivity.this, "吐司", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".ToolbarActivity">

    <Button
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:onClick="kevin"
        android:text="弹出popupwindow" />

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:background="@drawable/ic_launcher_background"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/btn_1"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:text="北京"/>
    <Button
        android:id="@+id/btn_2"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:text="上海"/>

</LinearLayout>

困死了
睡觉

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值