10、Android -- PopupWindow 基础学习

属性/设置

  • new PopupWindow(View contentView, int width, int height, boolean focusable) 新建PopupWindow
  • setContentView(View contentView);设置PopupWindow显示的View
  • showAsDropDown(view anchor);相对某个控件的位置(正左下方),无偏移
  • showAsDropDown(View anchor, int xoff, int yoff):相对某个控件anchor的位置,有偏移
  • showAtLocation(View parent, int gravity, int x, int y);相对父控件的位置,有偏移
  • setFocusable(boolean focusable)设置是否获取焦点
  • setBackgroundDrawable(Drawable background)设置背景
  • dismiss()关闭弹窗
  • setAnimationStyle(int animationStyle)设置加载动画
  • setTouchable(boolean touchable)设置触摸使能
  • setOutsideTouchable(boolean touchable)设置PopupWindow外面的触摸使能

备注:
1、new PopupWindow()根据参数个数有多个方法。
一般常用三个参数(new PopupWindow(View contentView, int width, int height))。
我的理解是,这个new相当于 setContentView,setWidth,setHeight,setFocusable的集合。

示例

1、res/layout/activity_popup.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".PopupActivity">
    <Button
        android:id="@+id/btn_pp_one"
        android:text="popupWindow 测试"
        android:onClick="TestPopupWindow"
        android:layout_width="200dp"
        android:layout_height="100dp"
        />
</LinearLayout>

2、res/layout/popupwidow.xml (进layout文件,没有对应的java)

<?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"
    android:background="@mipmap/ic_launcher">
    <Button
        android:id="@+id/btn_ppw_one"
        android:text="python学习"
        android:layout_margin="20px"
        android:layout_width="200dp"
        android:layout_height="100dp"

        />
    <Button
        android:id="@+id/btn_ppw_two"
        android:text="android学习"
        android:layout_margin="20px"
        android:layout_width="200dp"
        android:layout_height="100dp"
        />

</LinearLayout>

3、com.pha.first/PopupActivity.java

package com.pha.first;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;

public class PopupActivity extends AppCompatActivity {

    private static final String TAG = "PopupWindow";

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

    public void TestPopupWindow(View view) {
        View view1 = getLayoutInflater().inflate(R.layout.popupwindow, null);
        Button btn_ppw_one = view1.findViewById(R.id.btn_ppw_one);
        Button btn_ppw_two = view1.findViewById(R.id.btn_ppw_two);
        PopupWindow popupWindow = new PopupWindow(view1, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT,true);
        //popupWindow.showAtLocation(view, Gravity.LEFT,0,0);
        popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.popup));
        popupWindow.showAsDropDown(view,0,0);
        btn_ppw_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e(TAG, "onClick: 学习python");
            }
        });
        btn_ppw_two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e(TAG, "onClick: 学习android");
                popupWindow.dismiss();
            }
        });
    }
}

效果图

1、界面效果图
PopupWindow界面
2、Log.e 输出效果
PopupWindow-Log.e效果图

AlertDialog 和PoPupWindow 区别

相关AlertDialog
AlertDialog与PopupWindow区别

记忆知识点

1、View view1 = getLayoutInflater().inflate(R.layout.popupwindow, null); 获取view
2、getResources().getDrawable(R.drawable.popup);drawable中的图片转为drawable类型

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值