Android APP开发09(PopupWindow)

PopupWindow

弹出式窗口

常用方法

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

实例

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" xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical">
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:text="弹出窗口弹窗"
                android:onClick="leoclick"
        />
</LinearLayout>

java

package com.example.mypopupwindow;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void leoclick(View view){
        View popupview=getLayoutInflater().inflate(R.layout.popup_view,null);
        Button btn1=popupview.findViewById(R.id.btn1);
        Button btn2=popupview.findViewById(R.id.btn2);
        final PopupWindow popupWindow=new PopupWindow
                (popupview, ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT,true);
        popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.chunyu03));
        popupWindow.showAsDropDown(view,100,100);//显示在按钮下方有偏移
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e("leo","点击了江西按钮");
                popupWindow.dismiss();//关闭
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e("leo","点击了上海按钮");
                popupWindow.dismiss();//关闭
            }
        });
    }
}

新建一个popup_view.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"
        android:background="@mipmap/ic_launcher">
    <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:padding="5dp"
            android:text="江西"
            android:textSize="20sp"
            android:id="@+id/btn1"/>
    <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:padding="5dp"
            android:text="上海"
            android:textSize="20sp"
            android:id="@+id/btn2"/>
</LinearLayout>
效果图

在这里插入图片描述
点击按钮会进入对应的按钮点击事件执行操作
此处假设点击上海,控制台打印
在这里插入图片描述
PopupWindow测试练习就到此结束…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

碰磕

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值