001、仿微信右上角弹出菜单

一、效果图

二、代码

1、菜单布局

<?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">
    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginEnd="10dp"
        android:layout_weight="1"
        android:background="#393a3f"
        android:drawableStart="@drawable/album"
        android:drawablePadding="10dp"
        android:paddingStart="10dp"
        android:paddingEnd="10dp"
        android:text="发起群聊"
        android:textColor="#ffffff"
        android:textSize="18sp"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginEnd="10dp"
        android:layout_weight="1"
        android:background="#393a3f"
        android:drawableStart="@drawable/album"
        android:drawablePadding="10dp"
        android:paddingStart="10dp"
        android:paddingEnd="10dp"
        android:text="发起群聊"
        android:textColor="#ffffff"
        android:textSize="18sp"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginEnd="10dp"
        android:layout_weight="1"
        android:background="#393a3f"
        android:drawableStart="@drawable/album"
        android:drawablePadding="10dp"
        android:paddingStart="10dp"
        android:paddingEnd="10dp"
        android:text="发起群聊"
        android:textColor="#ffffff"
        android:textSize="18sp"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginEnd="10dp"
        android:layout_weight="1"
        android:background="#393a3f"
        android:drawableStart="@drawable/album"
        android:drawablePadding="10dp"
        android:paddingStart="10dp"
        android:paddingEnd="10dp"
        android:text="发起群聊"
        android:textColor="#ffffff"
        android:textSize="18sp"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginEnd="10dp"
        android:layout_weight="1"
        android:background="#393a3f"
        android:drawableStart="@drawable/album"
        android:drawablePadding="10dp"
        android:paddingStart="10dp"
        android:paddingEnd="10dp"
        android:text="发起群聊"
        android:textColor="#ffffff"
        android:textSize="18sp"/>
</LinearLayout>

2、主布局

<?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">
    <!--顶部标题栏-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal"
        android:background="#303030"
        android:gravity="center_vertical">
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginStart="10dp"
            android:text="微信"
            android:textSize="25sp"
            android:textColor="#ffffff"/>
        <TextView
            android:id="@+id/tv_plus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="15dp"
            android:gravity="end"
            android:text="+"
            android:textSize="40sp"
            android:textColor="#ffffff"/>
    </LinearLayout>
</LinearLayout>

3、逻辑代码

package com.example.administrator.app_book_test;

import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private ActionBar actionBar;
    private TextView tv_plus;
    PopupWindow popupWindow;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //隐藏系统标题栏
        actionBar = getSupportActionBar();
        hideActionBar();//调用隐藏ActionBar的方法
        tv_plus = findViewById(R.id.tv_plus);//初始化视图
        tv_plus.setOnClickListener(this);//点击事件
    }
    //隐藏ActionBar的方法
    private void hideActionBar() {
        actionBar.hide();
    }
    //点击事件汇总
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_plus:
                //点击事件
                showPopupWindow();
                break;
            case R.id.btn1:
                Toast.makeText(this, "按钮1被单击了", Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
                break;
        }
    }
    //显示popupWindow
    private void showPopupWindow(){
        //获取自定义菜单的布局文件
        final View popupWindow_view = getLayoutInflater().inflate(R.layout.wechat_menu_layout,null,false);
        //创建popupWindow,设置宽度和高度
        popupWindow = new PopupWindow(popupWindow_view,ActionBar.LayoutParams.WRAP_CONTENT,ActionBar.LayoutParams.WRAP_CONTENT,true);
        //内部控件的点击事件
        Button btn1 = popupWindow_view.findViewById(R.id.btn1);
        btn1.setOnClickListener(this);
        //设置菜单的显示位置
        popupWindow.showAsDropDown(tv_plus,0,0);
        //兼容5.0点击其他位置隐藏popupWindow
        popupWindow_view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                //必须写 v.performClick();解决与单击事件的冲突
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        //如果菜单不为空,且菜单正在显示
                        if(popupWindow!=null&&popupWindow.isShowing()){
                            popupWindow.dismiss();//隐藏菜单
                            popupWindow = null;//初始化菜单
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        view.performClick();
                        break;
                    default:
                        break;
                }
                return false;
            }
        });
    }
}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我会尽力回答你的问题。 要实现类似微信聊天消息长按弹出菜单的效果,可以使用 PopupWindow 组件来实现。下面是实现的大致步骤: 1. 在布局文件中定义一个包含要弹出菜单项的布局文件,例如一个 LinearLayout。 2. 在代码中创建 PopupWindow 对象,并设置其内容为上一步中定义的布局文件。 3. 设置 PopupWindow 的大小、位置、背景等属性。 4. 监听长按事件,在长按事件中弹出 PopupWindow。 5. 监听 PopupWindow 中菜单项的点击事件,处理相应的逻辑。 具体实现细节可以参考以下代码: ``` // 定义 PopupWindow PopupWindow popupWindow = new PopupWindow(context); View contentView = LayoutInflater.from(context).inflate(R.layout.menu_layout, null); popupWindow.setContentView(contentView); popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); // 监听长按事件 view.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { // 显示 PopupWindow int[] location = new int[2]; v.getLocationOnScreen(location); popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0], location[1] - popupWindow.getHeight()); return true; } }); // 监听菜单项的点击事件 TextView menuItem = contentView.findViewById(R.id.menu_item); menuItem.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 处理菜单项的逻辑 popupWindow.dismiss(); } }); ``` 希望这个回答能够帮到你,如果还有其他问题,可以继续问我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值