iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android使用popupWindow仿微信弹出框使用方法
  • 865
分享到

Android使用popupWindow仿微信弹出框使用方法

2024-04-02 19:04:59 865人浏览 安东尼
摘要

本文实例为大家分享了Android使用popupWindow仿微信弹出框的具体实现代码,供大家参考,具体内容如下 效果如下: 一、activity_main.xml代码 在acti

本文实例为大家分享了Android使用popupWindow仿微信弹出框的具体实现代码,供大家参考,具体内容如下

效果如下:

一、activity_main.xml代码

在activity_main.xml中设置"弹出框"按钮,并将activity_main.xml最外层设置一个id,代码如下

<androidx.drawerlayout.widget.DrawerLayout
    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:id="@+id/drawerLayout"
    tools:context=".MainActivity">
 
 
        <!-- 模拟toolbar的左侧图标 -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:orientation="horizontal"
            android:gravity="center_vertical"
            android:background="@color/colorPrimary">
            <Button
                android:id="@+id/img_menuBtn"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:text="侧边栏"
                android:padding="8dp"/>
 
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"/>
 
            <Button
                android:id="@+id/popupBtn"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:text="弹出框"
                android:padding="8dp"/>
        </LinearLayout>
 
</androidx.drawerlayout.widget.DrawerLayout>

二、创建带箭头的视图类

ArrowView.java代码如下:

package com.chy.test;
 
import android.content.Context;
import android.graphics.canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.LinearLayout;
 
 
import androidx.annotation.Nullable;
 
public class ArrowView extends LinearLayout {
    
    public ArrowView(Context context) {
        super(context);
    }
 
    public ArrowView(Context context,@Nullable AttributeSet attrs) {
        super(context,attrs);
    }
 
    public ArrowView(Context context,@Nullable AttributeSet attrs,int defStyleAttr) {
        super(context,attrs,defStyleAttr);
    }
 
    
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 创建画笔
        Paint paint = new Paint();
        paint.setAntiAlias(true);   //设置画笔抗锯齿
        paint.setStrokeWidth(2);    //设置线宽
        paint.setColor(Color.BLACK);  //设置线的颜色
 
        int height = getHeight();   //获取View的高度
        int width = getWidth();     //获取View的宽度
 
        
        
        RectF rectF = new RectF(getPaddingLeft() - 20,getPaddingTop() - 20,width - getPaddingRight() + 20,height - getPaddingBottom()+20);
 
        
        canvas.drawRoundRect(rectF,30,30,paint);
       
 
        // 三角形在视图的正下方
        
 
        
 
       
 
        // 三角形在视图的右上方
        Path path = new Path();
        //以下是绘制视图的那个箭头
        path.moveTo(width - getPaddingTop() * 3/2, 0);// 三角形顶点
        path.lineTo(width - getPaddingTop(),  getPaddingTop());   //三角形右边的点
        path.lineTo(width - getPaddingTop()*2,  getPaddingTop());   //三角形左边的点
 
        path.close();
        canvas.drawPath(path, paint);
        super.onDraw(canvas);
    }
}

三、创建popupwindow_dialog.xml和text.xml

popupwindow_dialog.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">
    <!--使用箭头视图-->
    <com.chy.test.ArrowView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/whiteColor"
        android:padding="20dp">
 
        <ListView
            android:id="@+id/lv_dialog"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:cacheColorHint="#00000000">
        </ListView>
 
    </com.chy.test.ArrowView>
 
</LinearLayout>

text.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" >
 
    <TextView
        android:id="@+id/tv_item"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dip"
        android:padding="5dp"
        android:textColor="@color/whiteColor"
        android:textSize="20sp" />
 
</LinearLayout>

四、使用方法

package com.chy.test;
 
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Toast;
 
public class MainActivity extends AppCompatActivity {
 
 
    private Button popupBtn;// 弹出框按钮
    private PopupWindow popupWindow;
    private LinearLayout layout;
    private ListView listView;
    private String[] add ={"发起群聊","添加朋友","视屏聊天","扫一扫","拍照分享"};
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        initViews();
    }
    
    private void initViews() {
        // 弹出框
        popupBtn = findViewById(R.id.popupBtn);
        popupClick();
    }
 
 
    
    private void popupClick(){
        popupBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int y1 = popupBtn.getBottom() * 3 / 2;
                int x1 = getWindowManager().getDefaultDisplay().getWidth();
                showAddPopupWindow(x1, y1);
            }
        });
    }
 
 
    
    public void showAddPopupWindow(int x, int y) {
        layout = (LinearLayout) LayoutInflater.from(MainActivity.this).inflate(
                R.layout.popupwindow_dialog, null);
        listView = layout.findViewById(R.id.lv_dialog);
        listView.setAdapter(new ArrayAdapter(MainActivity.this,
                R.layout.text, R.id.tv_item, add));
 
        popupWindow = new PopupWindow(MainActivity.this);
        // 以下两种选其一
        //popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setBackgroundDrawable(null);
 
        popupWindow
                .setWidth(getWindowManager().getDefaultDisplay().getWidth() / 2);
        popupWindow.setHeight(640);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);
        popupWindow.setContentView(layout);
        popupWindow.showAtLocation(findViewById(R.id.drawerLayout), Gravity.LEFT
                | Gravity.TOP, x, y);//需要指定Gravity,默认情况是center.
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                Toast.makeText(getBaseContext(), "您选择了:"+add[arg2],Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
                popupWindow = null;
            }
        });
    }
 
}

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

--结束END--

本文标题: Android使用popupWindow仿微信弹出框使用方法

本文链接: https://www.lsjlt.com/news/167939.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Android使用popupWindow仿微信弹出框使用方法
    本文实例为大家分享了Android使用popupWindow仿微信弹出框的具体实现代码,供大家参考,具体内容如下 效果如下: 一、activity_main.xml代码 在acti...
    99+
    2024-04-02
  • Android中使用PopupWindow 仿微信点赞和评论弹出
    微信朋友圈的点赞和评论功能,有2个组成部分:左下角的“更多”按钮;点击该按钮后弹出的对话框;PopupWindow,弹出框使用PopupWindow实现,这是点赞和评论的载体,具体要涉及 PopupWindow 点击非窗口位置和再次点击消失...
    99+
    2023-05-31
    android popupwindow roi
  • Android Popupwindow弹出窗口的简单使用方法
    本文实例为大家分享了Android Popupwindow弹出窗口的具体代码,供大家参考,具体内容如下代码很简单,没有和别的控件连用。布局自己随意定义,我的这个是最基础的,就直接上代码啦! 在MainActivity里import andr...
    99+
    2023-05-30
    android popupwindow 弹出窗口
  • Android中PopupWindow弹出式窗口使用方法详解
    本文实例为大家分享了Android中PopupWindow弹出式窗口使用的具体代码,供大家参考,具体内容如下 效果图如下: 实现代码如下: activity_popup_windo...
    99+
    2024-04-02
  • Android PopupWindow使用方法小结
    前几天要用到PopupWindow,一时竟想不起来怎么用,赶紧上网查了查,自己写了个demo,并在此记录一下PopupWindow的用法。使用场景PopupWindow,顾名思义,就是弹窗,在很多场景下都可以见到它。例如ActionBar/...
    99+
    2023-05-31
    android popupwindow roi
  • Android简单使用PopupWindow的方法
    本文实例为大家分享了Android简单使用PopupWindow的的具体代码,供大家参考,具体内容如下 思路 1.在res下面创建一个menu文件夹,并新建一个xml文件作为Poup...
    99+
    2024-04-02
  • 如何使用微信小程序开发弹出框
    小编给大家分享一下如何使用微信小程序开发弹出框,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!view class=&q...
    99+
    2024-04-02
  • 在Android中使用PopupWindow实现一个弹出分享功能
    在Android中使用PopupWindow实现一个弹出分享功能?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。代码package com.duanlian.po...
    99+
    2023-05-31
    android popupwindow roi
  • 如何在Android中使用PopupWindow下拉框
    如何在Android中使用PopupWindow下拉框?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。activity_main.xml布局<RelativeLayou...
    99+
    2023-05-30
    android popupwindow
  • PopupWindow使用方法详解
    学习了Android PopupWindow的使用技巧 和【Android UI设计与开发】7.底部菜单栏(四)PopupWindow 实现显示仿腾讯新闻底部弹出菜单,然后自己进行了一下研究,写一个总结,方便以后学习。效果图:PopupWi...
    99+
    2023-05-30
    popupwindow 使用
  • 如何在Android中使用PopupWindow制作一个自定义弹窗
    本篇文章给大家分享的是有关如何在Android中使用PopupWindow制作一个自定义弹窗,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。代码:PopupWindow ...
    99+
    2023-05-31
    android popupwindow
  • 微信小程序下拉框组件使用方法
    小程序有时需要使用下拉框选项,通常我会使用 picker 组件实现。pick 组件使用 mode 来区分类别,默认使用普通选择器就行。 除了上述方式,我们也可以通过自定义组件实现,代...
    99+
    2024-04-02
  • Android对话框使用方法详解
    对话框(Dialog)是Android系统在Activity或者其他组件运行过程中提供的一种提示机制。它可以帮助应用完成一些必要的提示功能,同时提供一些与用户交互的功能。 对话框分为...
    99+
    2024-04-02
  • html如何使用弹出框trigger选项
    小编给大家分享一下html如何使用弹出框trigger选项,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧! 默认情况下,单击触发...
    99+
    2024-04-02
  • 使用react+redux实现弹出框案例
    本文实例为大家分享了用react+redux实现弹出框案例的具体代码,供大家参考,具体内容如下 redux 实现弹出框案例 1、实现效果,点击显示按钮出现弹出框,点击关闭按钮隐藏弹出...
    99+
    2024-04-02
  • Android对话框如何使用方法
    Android中对话框的使用方法有以下几种:1. 使用AlertDialog.Builder创建对话框```javaAlertDia...
    99+
    2023-08-08
    Android
  • layui框架弹出层layer怎么使用
    使用Layui框架的弹出层(layer)可以通过以下步骤进行:1. 在HTML文件中引入Layui的样式和脚本文件:```html`...
    99+
    2023-09-22
    layui layer
  • Jquery弹出层插件ThickBox的使用方法
    要使用jQuery弹出层插件ThickBox,您需要按照以下步骤进行操作:1. 首先,下载并引入jQuery库和ThickBox插件...
    99+
    2023-08-17
    jQuery
  • 微信小程序开发中如何使用toast等弹框提示
    这篇文章主要为大家展示了“微信小程序开发中如何使用toast等弹框提示”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“微信小程序开发中如何使用toast等弹框提示...
    99+
    2024-04-02
  • 在android使用PopupWindow时出现返回键消失如何解决
    在android使用PopupWindow时出现返回键消失如何解决?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。preparePopup方法。 public void sh...
    99+
    2023-05-31
    popupwindow android roi
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作