转:仿QQ长按弹出功能菜单

时间:2021-07-15 16:32:11

android app普遍常用的弹出上下文菜单是列表式的。QQ列表,在上方弹出冒泡效果菜单,如图转:仿QQ长按弹出功能菜单

这里讲解一下如何写出这个效果:

图片资源:两张图片,可以拼接成如上效果。

首先,我们需要写布局文件: 
layout_pop.xml (mipmap是android studio 的引用图片的写法,以前常用@drawable/xxx)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"> <Button
android:id="@+id/bt_l"
style="@style/PopBtuton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/bg_pop_l_t"
android:text="复制" /> <Button
android:id="@+id/bt_r"
style="@style/PopBtuton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/bt_l"
android:background="@mipmap/bg_pop_r_t"
android:text="回复" /> </RelativeLayout>

布局效果 
转:仿QQ长按弹出功能菜单

接下来是重点: 
先贴代码 
PopOptionUtil.java

package com.mjc.popdemo;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow; /**
* Created by imac on 15/7/8.
*/
public class PopOptionUtil { private Context mContext;
private int popupWidth;
private int popupHeight;
private PopupWindow popupWindow;
private PopClickEvent mEvent;
private Button preBt;
private Button nextBt; public PopOptionUtil(Context context) {
mContext = context;
View popupView = LayoutInflater.from(mContext).inflate(R.layout.layout_pop, null);
preBt = (Button) popupView.findViewById(R.id.bt_l);
nextBt = (Button) popupView.findViewById(R.id.bt_r);
popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
popupWidth = popupView.getMeasuredWidth();
popupHeight = popupView.getMeasuredHeight();
} public void show(View view) {
initEvent();
int[] location = new int[2];
view.getLocationOnScreen(location);
popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, (location[0] + view.getWidth() / 2) - popupWidth / 2,
location[1] - popupHeight);
} public void setOnPopClickEvent(PopClickEvent mEvent) {
this.mEvent = mEvent;
} private void initEvent() {
if (mEvent != null) {
preBt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mEvent.onPreClick();
}
});
nextBt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mEvent.onNextClick();
}
}); }
}
}

其中Event是自定义的接口,用来监听两个Button的事件的

下面就是 
PopClickEvent.java

public interface PopClickEvent {
public void onPreClick();
public void onNextClick();
}

使用方法:

package com.mjc.popdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast; public class MainActivity extends AppCompatActivity { View bt;
PopOptionUtil mPop; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = findViewById(R.id.test);
mPop = new PopOptionUtil(this);
mPop.setOnPopClickEvent(new PopClickEvent() {
@Override
public void onPreClick() {
Toast.makeText(MainActivity.this,"置顶",Toast.LENGTH_SHORT).show();
} @Override
public void onNextClick() {
Toast.makeText(MainActivity.this,"删除",Toast.LENGTH_SHORT).show();
}
});
bt.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
mPop.show(view);
bt.setSelected(true);
return true;
}
});
} }

最终效果: 
转:仿QQ长按弹出功能菜单

是不是很简单的呢?快动手试试吧

参考:http://blog.csdn.net/u011494050/article/details/38691475

http://blog.csdn.net/meijian531161724/article/details/46804281