Android之PopupWindow

本文详细探讨了Android中PopupWindow的使用,包括如何在PopupWindowActivity中集成和配置PopupWindow,以及它在页面布局file layout_pop.xml中的实现细节。

PopupWindowActivity以及页面布局文件

package com.example.test0508;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.test0508.util.ToastUtil;

public class PopupWindowActivity extends AppCompatActivity {

    private Button mBtnPop;

    private PopupWindow mPopWindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_popup_window);
        mBtnPop = findViewById(R.id.btn_pop);
        mBtnPop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View view = getLayoutInflater().inflate(R.layout.layout_pop,null);

                TextView textView = view.findViewById(R.id.tv_pop_good);
                textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mPopWindow.dismiss();
                        ToastUtil.showMessage(PopupWindowActivity.this,"点击了好");
                    }
                });

                mPopWindow = new PopupWindow(view,mBtnPop.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT);
                mPopWindow.setOutsideTouchable(true);
                mPopWindow.setFocusable(true);
                mPopWindow.setBackgroundDrawable(new BitmapDrawable());
                mPopWindow.showAsDropDown(mBtnPop);
            }
        });
    }
}

<?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:id="@+id/btn_pop"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:text="POP"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="50dp"
    />
</LinearLayout>

在这里插入图片描述


layout_pop.xml

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

    <TextView
        android:id="@+id/tv_pop_good"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="好"
        android:textSize="20sp"
        android:gravity="center"
        android:paddingTop="8dp"
        android:paddingBottom="8dp"
        />

    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="@color/colorGreen"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="坏"
        android:textSize="20sp"
        android:gravity="center"
        android:paddingTop="8dp"
        android:paddingBottom="8dp"
        />
    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="@color/colorGreen"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="还行"
        android:textSize="20sp"
        android:gravity="center"
        android:paddingTop="8dp"
        android:paddingBottom="8dp"
        />
    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="@color/colorGreen"
        />
</LinearLayout>

在这里插入图片描述

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值