【13】Android UI控件 - PopupWindow

前言

不知道你有没有试过,在微信的聊天列表长按与某人的聊天表项目,这是会在我们长按的地方下弹出一个小列表。今天我们要学的 PopupWindow 就能实现这样的效果。

在官方API中有这样一句话介绍它:This class represents a popup window that can be used to display an arbitrary view. The popup window is a floating container that appears on top of the current activity.

常用方法

setContentView(View contentView)

设置PopupWindow显示的View

 

getContentView()

获得PopupWindow显示的View

 

showAsDropDown(View anchor)

相对某个控件的位置(正左下方),无偏移

 

showAsDropDown(View anchor, int xoff, int yoff)

相对某个控件的位置,有偏移

 

showAtLocation(View parent, int gravity, int x, int y)

相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移

 

setWidth / setHeight

设置宽高,也可以在构造方法那里指定好宽高

 

setFocusable(true)

设置焦点,PopupWindow 弹出后,所有的触屏和物理按键都由 PopupWindows 处理

其他任何事件的响应都必须发生在 PopupWindow 消失之后,(home 等系统层面的事件除外)

 

setAnimationStyle(int)

设置动画效果

代码示例

主界面

activity_popup.xml

<?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"
    tools:context=".Popup_Activity"
    android:gravity="center">

    <Button
        android:id="@+id/PopButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="POPWindow"/>


</LinearLayout>

PopupWindow界面

layout_popup.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:gravity="center|top"
    android:background="@mipmap/backpng">

    <TextView
        android:id="@+id/tw_green"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text = "我绿了"
        android:textColor="@color/colorPrimary"
        android:gravity="center"
        android:paddingTop="20dp"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="@color/colorPrimary"/>

    <TextView
        android:id="@+id/tw_yellow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text = "我黄了"
        android:textColor="@color/colorPrimary"
        android:gravity="center"
        android:paddingTop="20dp"/>


</LinearLayout>

Java代码

Popup_Activity.java

package com.example.mytest_button;

import androidx.appcompat.app.AppCompatActivity;

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

public class Popup_Activity extends AppCompatActivity {

    private Button Btn_pop;
    private PopupWindow m_pop;

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

        Btn_pop = findViewById(R.id.PopButton);
        //设置按键点击事件监听器
        Btn_pop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //类似于findViewById,不过它是用来找布局xml文件的
                View v = getLayoutInflater().inflate(R.layout.layout_popup,null);

                //实例化popup-window
                m_pop = new PopupWindow(v,Btn_pop.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT);
                //popup_up的一些属性设置方法;Android4.1运行无效,官方API未找到Android版本引起的失效
                m_pop.setOutsideTouchable(true);
                m_pop.setFocusable(true);
                //还可以设置动画
                //在按钮下方显示popup-window
                m_pop.showAsDropDown(Btn_pop);

                //popup-window选项1点击事件
                TextView textView1 = v.findViewById(R.id.tw_green);
                textView1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //popup消失
                        m_pop.dismiss();
                        //其他动作-变色
                        Btn_pop.setTextColor(Color.GREEN);
                    }
                });

                //popup-window选项2点击事件
                TextView textView2 = v.findViewById(R.id.tw_yellow);
                textView2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        m_pop.dismiss();
                        Btn_pop.setTextColor(Color.YELLOW);
                    }
                });

            }
        });
    }
}

解析

1)首先我们应该创建两个视图(xml文件),一个用来做主界面,一个用来做 PopupWindow 界面

2)这里我在主界面的xml中设置了一个 Button ,在 PopupWindow小界面 中添加了两个 TextView 和一根线将两个 TextView 分开。

3)在调用主界面显示的 Popup_Activity.java 中设置按键点击监听器,按键点击处理即是实例化PopupWindow,并显示在Button的下方。

4)随后我们添加PopupWindowTextView的点击事件监听器。这里我的设置是让按键字体变色。

效果图

 

疑点:本人开始使用setOutsideTouchable(true),在Android4.1的虚拟机上测试是没有效果的,后来换了Android8.0就有效了,跑到API中查看,发现这个方法API3就开始使用了,应该不存在无效的问题...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值