仿QQ右上角的弹出菜单框
效果图
xml中
<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="com.lwy.lpopupmenu.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#FF1895FF">
<TextView
android:id="@+id/title_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="标题"
android:textColor="#ffffff"
android:textSize="16sp"/>
<ImageView
android:id="@+id/menu_iv"
android:layout_width="23dp"
android:layout_height="23dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="8dp"
android:background="@mipmap/common_forward_normal"/>
</RelativeLayout>
</LinearLayout>
Mactivity中
package com.example.popuwindow3;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.lwy.righttopmenu.MenuItem;
import com.lwy.righttopmenu.RightTopMenu;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private Button mBt;
private RightTopMenu mRightTopMenu;
private View mMenuIV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMenuIV = findViewById(R.id.menu_iv);
mMenuIV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<MenuItem> menuItems= new ArrayList<>();
menuItems.add(new MenuItem(R.mipmap.multichat, "发起多人聊天", 100));
menuItems.add(new MenuItem(R.mipmap.addmember, "加好友", 3));
menuItems.add(new MenuItem(R.mipmap.qr_scan, "扫一扫"));
menuItems.add(new MenuItem(R.mipmap.facetoface, "面对面快传"));
menuItems.add(new MenuItem(R.mipmap.pay, "付款"));
if (mRightTopMenu == null) {
mRightTopMenu = new RightTopMenu.Builder(MainActivity.this)
// .windowHeight(480) //当菜单数量大于3个时,为wrap_content,反之取默认高度320
// .windowWidth(320) //默认宽度wrap_content
.dimBackground(true) //背景变暗,默认为true
.needAnimationStyle(true) //显示动画,默认为true
.animationStyle(R.style.RTM_ANIM_STYLE) //默认为R.style.RTM_ANIM_STYLE
.menuItems(menuItems)
.onMenuItemClickListener(new RightTopMenu.OnMenuItemClickListener() {
@Override
public void onMenuItemClick(int position) {
Toast.makeText(MainActivity.this, "点击菜单:" + position, Toast.LENGTH_SHORT).show();
}
}).build();
}
mRightTopMenu.showAsDropDown(mMenuIV, 0, 0);
}
});
}
}