Android中PopupWindow弹出式窗口使用方法详解

本文实例为大家分享了Android中PopupWindow弹出式窗口使用的具体代码,供大家参考,具体内容如下

效果图如下:

实现代码如下:

activity_popup_window.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"
    android:orientation="vertical"
    tools:context=".PopupWindowActivity">

    <Button
        android:id="@+id/btn_popupWindow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="PopupWindow" />
</LinearLayout>

自定义弹出的视图layout_pop.xml,也可以用RecycleView或者ListView

<?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">

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

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

    <TextView
        android:id="@+id/tv_not_too_bad"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:paddingTop="8dp"
        android:paddingBottom="8dp"
        android:text="还行"
        android:textColor="@color/gray"
        android:textSize="20sp" />

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

    <TextView
        android:id="@+id/tv_bad"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:paddingTop="8dp"
        android:paddingBottom="8dp"
        android:text="不好"
        android:textColor="@color/gray"
        android:textSize="20sp" />
</LinearLayout>

PopupWindowActivity类实现代码如下:

public class PopupWindowActivity extends AppCompatActivity {
    private Button btn_popupWindow;
    private PopupWindow popupWindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_popup_window);
        btn_popupWindow = findViewById(R.id.btn_popupWindow);
        btn_popupWindow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                View popup_view = LayoutInflater.from(PopupWindowActivity.this).inflate(R.layout.layout_pop, null);
                TextView textView = popup_view.findViewById(R.id.tv_good);
                textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        popupWindow.dismiss();
                        Toast.makeText(PopupWindowActivity.this, "好", Toast.LENGTH_SHORT).show();
                    }
                });
                popupWindow = new PopupWindow(popup_view, btn_popupWindow.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT);
                //设置弹出窗口应该接收外部触摸事件
                popupWindow.setOutsideTouchable(true);
                //设置可聚焦
                popupWindow.setFocusable(true);
                popupWindow.showAsDropDown(btn_popupWindow);
            }
        });
    }
}

以上就是PopupWindow弹出式窗口的简单使用。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android自定义弹出窗口PopupWindow使用技巧

    PopupWindow是Android上自定义弹出窗口,使用起来很方便. PopupWindow的构造函数为 复制代码 代码如下: public PopupWindow(View contentView, int width, int height, boolean focusable) contentView为要显示的view,width和height为宽和高,值为像素值,也可以是MATCHT_PARENT和WRAP_CONTENT. focusable为是否可以获得焦点,这是一个很重要的参数

  • Android开发实现popupWindow弹出窗口自定义布局与位置控制方法

    本文实例讲述了Android开发实现popupWindow弹出窗口自定义布局与位置控制方法.分享给大家供大家参考,具体如下: 布局文件: 主布局文件:activity_main: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&q

  • Android控件PopupWindow模仿ios底部弹窗

    前言 在H5火热的时代,许多框架都出了底部弹窗的控件,在H5被称为弹出菜单ActionSheet,今天我们也来模仿一个ios的底部弹窗,取材于苹果QQ的选择头像功能. 正文 废话不多说,先来个今天要实现的效果图 整个PopupWindow的开启代码 private void openPopupWindow(View v) { //防止重复按按钮 if (popupWindow != null && popupWindow.isShowing()) { return; } //设置Popup

  • Android弹窗ListPopupWindow的简单应用详解

    概述 常用的弹窗有菜单,或者Dialog,但更加人性化和可自定义的还是PopupWindow 如果只是展示列表数据或者弹窗列表选择,直接使用ListPopupWindow即可,不用再单独去设置布局. 如果想要更加多样化的那就自定义一个布局,使用PopupWindow即可,也不复杂. 用法 自定义ListPopupWindow类 public class ChargeItemSumPop extends ListPopupWindow { public ChargeItemSumPop(Conte

  • Android编程实现的自定义弹窗(PopupWindow)功能示例

    本文实例讲述了Android编程实现的自定义弹窗(PopupWindow)功能.分享给大家供大家参考,具体如下: 在开发过程中,如果要弹出一个对话框,一般是使用AlertDialog,但其使用限制太大,灵活性不够,所以我们常需要用到灵活性更高的PopupWindow, 如图,当点击显示的时候,就会弹出一个对话框,当点击确定或屏幕其它任意地方,就可以将PopupWindow取消了,接下来贴出重要代码. PopupWindow pw = new PopupWindow(view.getContext

  • Android PopupWindow实现左侧弹窗效果

    本文实例为大家分享了Android PopupWindow实现左侧弹窗的具体代码,供大家参考,具体内容如下 效果图: MainActivity.java页面核心代码: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //在setContentView之前添加,未添加的话home键监听无效,设置窗体属性 this.getWindow().setFlags(0x80000

  • Android Popupwindow弹出窗口的简单使用方法

    本文实例为大家分享了Android Popupwindow弹出窗口的具体代码,供大家参考,具体内容如下 代码很简单,没有和别的控件连用.布局自己随意定义,我的这个是最基础的,就直接上代码啦! 在MainActivity里 import android.content.Context; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.LayoutInflat

  • Android使用 PopupWindow 实现底部弹窗功能

    一.知识点 不详细展开 PopupWindow 或者视图动画的所有具体使用方式,仅仅介绍一下使用的一个大概流程和一些知识要点,具体的介绍在下面设计实现中讲述 (一)PopupWindow 1. 初始化 加载弹窗的布局 实例化 PopupWindow 传入布局和弹窗的宽高 对布局里面的控件的操作 对布局本身的一些设置 // 加载弹窗的布局 pwView = LayoutInflater.from(this).inflate(R.layout.pw_search_engine, null, fals

  • Android开发之PopupWindow实现弹窗效果

    本文实例为大家分享了Android开发之PopupWindow实现弹窗的具体代码,供大家参考,具体内容如下 基本框架 在activity_main.xml中设置一个按钮,用于唤出弹窗; <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android

  • android PopupWindow 和 Activity弹出窗口实现方式

    本人小菜一个.目前只见过两种弹出框的实现方式,第一种是最常见的PopupWindow,第二种也就是Activity的方式是前几天才见识过.感觉很霸气哦.没想到,activity也可以做伪窗口. 先贴上最常见的方法,主要讲activity的方法. 一.弹出PopupWindow 复制代码 代码如下: /** * 弹出menu菜单 */ public void menu_press(){ if(!menu_display){ //获取LayoutInflater实例 inflater = (Layo

随机推荐