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

本文实例为大家分享了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的宽度
 
        /**视图显示的区域**/
        /**
         * @param left 长方形左侧的x坐标
         * @param top 长方形顶的Y坐标
         * @param right 长方形右侧的X坐标
         * @param bottom 长方形底的Y坐标*/
        RectF rectF = new RectF(getPaddingLeft() - 20,getPaddingTop() - 20,width - getPaddingRight() + 20,height - getPaddingBottom()+20);
 
        /**
         * @param rectF RectF对象
         * @param rx x方向上的圆角半径
         * @param ry y方向上的圆角半径
         * @param paint 绘制时所使用的画笔
         * */
        canvas.drawRoundRect(rectF,30,30,paint);
       /* // 三角形在视图的正上方
        Path path = new Path();
        //以下是绘制视图的那个箭头
        path.moveTo(width / 2, 0);// 三角形顶点
        path.lineTo(width / 2 + getPaddingTop(),  getPaddingTop());   //三角形右边的点
        path.lineTo(width / 2 - getPaddingTop(),  getPaddingTop());   //三角形左边的点*/
 
        // 三角形在视图的正下方
        /*Path path = new Path();
        //以下是绘制视图的那个箭头
        path.moveTo(width / 2, height);// 三角形顶点
        path.lineTo(width / 2 - getPaddingBottom(), height - getPaddingBottom());   //三角形左边的点
        path.lineTo(width / 2 + getPaddingBottom(), height - getPaddingBottom());   //三角形右边的点*/
 
        /*// 三角形在视图的正左方
        Path path = new Path();
        //以下是绘制视图的那个箭头
        path.moveTo(0,  height / 2);// 三角形左侧顶点
        path.lineTo(getPaddingLeft(), height / 2 - getPaddingLeft());   //三角形上面的点
        path.lineTo(getPaddingLeft(), height / 2 + getPaddingLeft());   //三角形下面的点*/
 
       /* // 三角形在视图的正右方
        Path path = new Path();
        //以下是绘制视图的那个箭头
        path.moveTo(width,  height / 2);// 三角形右侧顶点
        path.lineTo(width - getPaddingRight(), height / 2 - getPaddingRight());   //三角形上面的点
        path.lineTo(width - getPaddingRight(), height / 2 + getPaddingRight());   //三角形下面的点*/
 
        // 三角形在视图的右上方
        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);
            }
        });
    }
 
 
    /**
     * 点击popupBtn按钮时弹出popuwindow
     */
    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;
            }
        });
    }
 
}

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

(0)

相关推荐

  • Android studio实现PopupWindow弹出框效果

    本文实例为大家分享了Android studio实现PopupWindow弹出框的具体代码,供大家参考,具体内容如下 实现步骤: 第一步:自定义.xml布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width=&q

  • Android实现底部半透明弹出框PopUpWindow效果

    Android底部半透明弹出框PopUpWindow,供大家参考,具体内容如下 layout布局: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" androi

  • Android使用popUpWindow带遮罩层的弹出框

    上次项目中实现了新功能,就一直想添加到博客里来着,惰性发作起来简直太可怕,不说了,跟着一起写吧,三步即可实现简单的弹出框功能,首先看效果-- 首先:主页面布局,触发控件一定要有,再有就是给根标签设置id <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:

  • Android中自定义PopupWindow实现弹出框并带有动画效果

    使用PopupWindow来实现弹出框,并且带有动画效果 首先自定义PopupWindow public class LostPopupWindow extends PopupWindow { public Lost lost; public void onLost(Lost lost){ this.lost = lost; } private View conentView; public View getConentView() { return conentView; } public L

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

    本文实例为大家分享了Android使用popupWindow仿微信弹出框的具体实现代码,供大家参考,具体内容如下 效果如下: 一.activity_main.xml代码 在activity_main.xml中设置"弹出框"按钮,并将activity_main.xml最外层设置一个id,代码如下 <androidx.drawerlayout.widget.DrawerLayout     xmlns:android="http://schemas.android.com/

  • Android自定义view仿iOS弹出框效果

    本文实例为大家分享了Android自定义view仿iOS弹出框的具体代码,供大家参考,具体内容如下 运行效果图 自定义对话框的使用,仿照ios.从底部弹出,类似pop窗口.包括消息.图片.列表及对话框. 好了,用法都会,直接贴上代码 1.layout布局文件 view_actionsheet.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="ht

  • jQuery仿IOS弹出框插件

    这款弹出框插件是本人自己模仿IOS原生弹出框写的一个自定义插件,包括Alert弹出框和Confirm确认框,支持标题,内容,按钮文字以及按钮回调函数自定义,使用非常简单,可以进行扩展,下面是插件使用方法,一看就懂: 首先导入jquery库和插件库以及css文件 <script src="./jquery-1.8.3.min.js"></script> <script src="./jquery.confirm-1.1.js">&l

  • Android自定义弹出框的方法

    在开发Android项目的过程中,弹出框真的是我们的常见的一种互动式窗体,但是Android系统自带的弹出框往往都不能满足我们的需要,大多数的时候需要我们自定义一种更漂亮的窗体来来展示给用户. 接下来是我很久之前用的一个自定义弹出框,记录一下,以便自己日后使用. 0.先来一张效果图 1.先定义个一个继承自Dialog的自定义弹框CustomDialog import android.app.Dialog; import android.content.Context; import androi

  • js实现遮罩层弹出框的方法

    本文实例讲述了js实现遮罩层弹出框的方法.分享给大家供大家参考.具体分析如下: 昨天公司网站需要弹窗提示一些信息,要我在把弹窗的js代码和弹窗窗口html写在一起哪里需要就调用 不说那么多了,直接上代码,感觉肯定会有兼容问题,看到了请指出啊: 复制代码 代码如下: <style>     #H-dialog{display:none;position:absolute;z-index: 9999999;width:400px;height: auto; background-color: #f

  • EasyUI实现第二层弹出框的方法

    这是网友在EasyUI拓展时的代码. 可以根据实际需要自行修改. $.modalDialog2.handler 这个handler代表弹出的dialog句柄  $.modalDialog2.xxx 这个xxx是可以自己定义名称,主要用在弹窗关闭时,刷新某些对象的操作,可以将xxx这个对象预定义好 复制代码 代码如下: $.modalDialogTwo = function(options) {  if ($.modalDialogTwo.handler == undefined) {// 避免重

  • Android编程实现AlertDialog自定义弹出对话框的方法示例

    本文实例讲述了Android编程实现AlertDialog自定义弹出对话框的方法.分享给大家供大家参考,具体如下: 弹出对话框,显示自定义的布局文件 弹出对话框提示设置密码或登录密码 private void showSetPasswordDialod(){ View dialogView=mInflater.inflate(R.layout.protect_first_dialog, null); AlertDialog.Builder builder=new AlertDialog.Buil

  • layui 优化button按钮和弹出框的方法

    正常下的layui在3个button按钮 压缩之后 代码为 <button data-id="{$activity.activity_id}" style="background-color:#297911;" class="layui-btn layui-btn-small layui-btn-danger edit_store">修改门店</button> <button data-id="{$acti

  • layer.confirm点击第一个按钮关闭弹出框的方法

    layer.confirm弹出框,当你点击第一个按钮的时候,这个弹出层不会关闭,也是给一些小伙伴带来了一些困扰, 下面分享两个解决方案: ①可以将第一个按钮的回调函数传一个index,然后使用layer.close("index")就可以点击第一个按钮的时候关闭弹出框了,代码如下: ②第二种方法就非常的粗暴了,你可以直接在这个回调函数中写: layer.closeAll('dialog'); 也能实现点击第一个按钮的时候关闭弹出框 以上这篇layer.confirm点击第一个按钮关闭弹

  • Android实现可输入数据的弹出框

    之前一篇文章,介绍了如何定义从屏幕底部弹出PopupWindow即<Android Animation实战之屏幕底部弹出PopupWindow>,写完之后,突然想起之前写过自定义内容显示的弹出框,就随手写了两个实例,分享出来: 第一种实现方式:继承Dialog  1.1 线定义弹出框要显示的内容:create_user_dialog.xml <?xml version="1.0" encoding="utf-8"?> <LinearLa

随机推荐