Android自定义PopWindow实现QQ、微信弹出菜单效果

前段时间在个人开发的项目中需要用到弹出菜单,类似QQ右上角的弹出菜单,自己使用popwin的次数也不是很多,其中也遇到过一点问题,今天正好有时间就把一些经验分享给大家。

先来看看最终实现过后的效果怎么样,下面放上图

自定义的弹出菜单是继承的popwin,并不是view 因为没有必要重复造车轮,如果想要实现某种特殊的效果另说。首先创建类MyPopWindow继承Popwindow。

public class MyPopWindow extends PopupWindow implements View.OnClickListener {
 private Context context;
 private View view;
 private LinearLayout scan;
 private LinearLayout add;
 public MyPopWindow(Context context) {
 this(context, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
 }

 public MyPopWindow(Context context, int width, int height) {
 super(context);
 this.context = context;
 setWidth(width);
 setHeight(height);
 setFocusable(true);
 setOutsideTouchable(true);
 setTouchable(true);
 view = LayoutInflater.from(context).inflate(R.layout.layout_mypopwin,null);
 setContentView(view);
 scan = (LinearLayout) view.findViewById(R.id.scan);
 add = (LinearLayout) view.findViewById(R.id.add);
 }

 @Override
 public void onClick(View view) {
 switch (view.getId()){
  case R.id.scan:

  break;
  case R.id.add:

  break;
 }
 }
}

下面给出最开始的布局文件

<?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">
 <LinearLayout
 android:id="@+id/scan"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content">
 <TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="扫一扫"
  android:textSize="16sp"/>
 </LinearLayout>
 <View
 android:layout_width="wrap_content"
 android:layout_height="0.5dp"
 android:background="#cdcdcd"/>
 <LinearLayout
 android:id="@+id/add"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content">
 <TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="加好友"
  android:textSize="16sp"/>
 </LinearLayout>
</LinearLayout>

在activity中调用自定义弹出菜单看看目前的效果

调用的代码MyPopWindow win = new MyPopWindow (MainActivity.this, 200,150);指定了弹出菜单的宽高,如果不给就会默认给出wrap_content,就会沾满整个屏幕的宽度。这个样子还是比较简陋,现在在布局文件中加上.9图的背景图,在来看看效果

<?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"
 android:background="@drawable/title_function_bg">
 <LinearLayout
 android:id="@+id/scan"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content">
 <TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="扫一扫"
  android:textSize="16sp"/>
 </LinearLayout>
 <View
 android:layout_width="wrap_content"
 android:layout_height="0.5dp"
 android:background="#cdcdcd"/>
 <LinearLayout
 android:id="@+id/add"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content">
 <TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="加好友"
  android:textSize="16sp"/>
 </LinearLayout>
</LinearLayout>

运行后的效果

美观了一点,但是背景后面还有背景什么情况,那么问题来了,怎么解决这个问题?那就需要在popwin的构造方法中加入setBackgroundDrawable(new BitmapDrawable()),难看的方形背景就会消失了。

接近目标效果了,现在的问题是,每次增加一个菜单项都要手动的定制宽高很烦人,想让它自己适应高度、宽度,所以那就得修改布局文件了,想想android能够自由增加item的控件不少,首先想到的就是listview。修改布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:background="@drawable/title_function_bg">
 <ListView
 android:id="@+id/title_list"
 android:layout_width="120dp"
 android:layout_height="fill_parent"
 android:cacheColorHint="#00000000"
 android:divider="@drawable/popu_line"
 android:padding="3dp"
 android:scrollingCache="false"
 android:listSelector="@drawable/title_list_selector"/>
</LinearLayout>

然后修改自定义的popwindow

public class CustomWin extends PopupWindow {
 private Context context;
 private View view;
 private ListView listView;
 private List<String> list;

 public CustomWin(Context context) {
 this(context, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
 }

 public CustomWin(Context context, int with, int height) {
 this.context = context;
 setWidth(with);
 setHeight(height);
 //设置可以获得焦点
 setFocusable(true);
 //设置弹窗内可点击
 setTouchable(true);
 //设置弹窗外可点击
 setOutsideTouchable(true);
 setBackgroundDrawable(new BitmapDrawable());
 view = LayoutInflater.from(context).inflate(R.layout.popwin_menu,null);
 setContentView(view);
 setAnimationStyle(R.style.popwin_anim_style);
 initData();
 }

 private void initData() {
 listView = (ListView) view.findViewById(R.id.title_list);
 list = new ArrayList<String>();
 list.add("添加好友");
 list.add("扫一扫");
 list.add("支付宝");
 list.add("视频聊天");
 //设置列表的适配器
 listView.setAdapter(new BaseAdapter() {
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
 TextView textView = null;

 if (convertView == null) {
  textView = new TextView(context);
  textView.setTextColor(Color.rgb(255,255,255));
  textView.setTextSize(14);
  //设置文本居中
  textView.setGravity(Gravity.CENTER);
  //设置文本域的范围
  textView.setPadding(0, 13, 0, 13);
  //设置文本在一行内显示(不换行)
  textView.setSingleLine(true);
 } else {
  textView = (TextView) convertView;
 }
 //设置文本文字
 textView.setText(list.get(position));
 //设置文字与图标的间隔
 textView.setCompoundDrawablePadding(0);
 //设置在文字的左边放一个图标
 textView.setCompoundDrawablesWithIntrinsicBounds(R.mipmap., null, null, null);

 return textView;
 }

 @Override
 public long getItemId(int position) {
 return position;
 }

 @Override
 public Object getItem(int position) {
 return list.get(position);
 }

 @Override
 public int getCount() {
 return list.size();
 }
 });
 }
}

最终效果:

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

您可能感兴趣的文章:

  • Android PopupWindow实现微信右上角的弹出菜单
  • Android仿微信长按菜单效果
  • Android仿微信底部菜单栏效果
  • Android制作微信app顶部menu菜单(ActionBar)
  • Android中微信小程序开发之弹出菜单
  • android自定义popupwindow仿微信右上角弹出菜单效果
  • Android开发Popwindow仿微信右上角下拉菜单实例代码
  • Android开发之微信底部菜单栏实现的几种方法汇总
  • Android仿微信底部菜单栏功能显示未读消息数量
  • Android仿微信滑动弹出编辑、删除菜单效果、增加下拉刷新功能
  • Android仿微信加号菜单模式
(0)

相关推荐

  • Android开发Popwindow仿微信右上角下拉菜单实例代码

    先给大家看下效果图: MenuPopwindow: package com.cloudeye.android.cloudeye.view; import android.app.Activity; import android.content.Context; import android.graphics.drawable.ColorDrawable; import android.view.LayoutInflater; import android.view.View; import an

  • Android仿微信底部菜单栏功能显示未读消息数量

    底部菜单栏很重要,我看了一下很多应用软件都是用了底部菜单栏,这里使用了tabhost做了一种通用的(就是可以像微信那样显示未读消息数量的,虽然之前也做过但是layout下的xml写的太臃肿,这里去掉了很多不必要的层,个人看起来还是不错的,所以贴出来方便以后使用). 先看一下做出来之后的效果: 以后使用的时候就可以换成自己项目的图片和字体了,主框架不用变哈哈, 首先是要布局layout下xml文件 main.xml: <?xml version="1.0" encoding=&qu

  • Android中微信小程序开发之弹出菜单

    先给大家展示下效果图,具体效果图如下所示: 具体代码如下所示: 1.index.js //index.js //获取应用实例 var app = getApp() Page({ data: { isPopping: false,//是否已经弹出 animationPlus: {},//旋转动画 animationcollect: {},//item位移,透明度 animationTranspond: {},//item位移,透明度 animationInput: {},//item位移,透明度

  • Android PopupWindow实现微信右上角的弹出菜单

    日常开发过程中对于PopupWindown的使用也是比较多的.这里给大家展示一下PopupWindow的使用. 修改activity_main.xml布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="matc

  • android自定义popupwindow仿微信右上角弹出菜单效果

    微信右上角的操作菜单看起来很好用,就照着仿了一下,不过是旧版微信的,手里刚好有一些旧版微信的资源图标,给大家分享一下. 不知道微信是用什么实现的,我使用popupwindow来实现,主要分为几块内容: 1.窗口布局文件:popwin_share.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com

  • Android仿微信底部菜单栏效果

    前言 在市面上,大多数的APP都需要通过底部菜单栏来将程序的功能进行分类整理,通常都是分为3-5个大模块,从而正确有效地引导用户去使用我们的APP.实现底部菜单栏的方法也有很多种. 1.仿微信底部菜单栏(ViewPager+ImagerView+TextView) ......(其他方式后续会补充) 效果预览 首先来个开胃菜,看看实现效果: 先贴出项目所需的资源文件,这些可随个人自由更改颜色和文字 colors.xml <color name="bg_line_light_gray&quo

  • Android仿微信滑动弹出编辑、删除菜单效果、增加下拉刷新功能

    如何为不同的list item呈现不同的菜单,本文实例就为大家介绍了Android仿微信或QQ滑动弹出编辑.删除菜单效果.增加下拉刷新等功能的实现,分享给大家供大家参考,具体内容如下 效果图: 1. 下载开源项目,并将其中的liberary导入到自己的项目中: 2. 使用SwipeMenuListView代替ListView,在页面中布局: <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swipeRefresh

  • Android开发之微信底部菜单栏实现的几种方法汇总

     实现方式 实现的方式有很多种 这里总结最常见的几种方式,以后再添加其他的. viewPager + RadioGroup viewPager + FragmentTabHost viewpager +TabLayout viewPager+RadioGroup 感觉这是最简单的一个了,我也就不贴代码 说说我理解的思路吧 通过给pager 和RadioGroup 添加监听,监听两个控件的变化 实现联动 当viewPager的显示pager改变就会触发监听 ,在监听中选中对应的RadioButto

  • Android仿微信长按菜单效果

    本文实例为大家分享了Android仿微信长按菜单展示的具体代码,供大家参考,具体内容如下 FloatMenu A menu style pop-up window that mimics WeChat.仿微信的长按菜单. 效果如下 引入方法: Github地址:https://github.com/JavaNoober/FloatMenu dependencies { .... compile 'com.noober.floatmenu:common:1.0.2' } 使用说明 使用方法1: A

  • Android仿微信加号菜单模式

    在模仿微信过程中有一个加号菜单启动着实让我有点费心,因为我去掉了自带的标题栏,想通过OnCreateOptionMenu这段代码来实现传统的Menu显示显然是不可能了.所以在自定义创建的状态栏里添加了一个加号的ImageView,想通过监听ImageView的Onclick来触发Popumenu的创建.基本效果与微信相似,细节方面还需多多考究. 看具体代码如下: 1.监听之后创建Popumenu的java代码: menuView.setOnClickListener(new View.OnCli

  • Android制作微信app顶部menu菜单(ActionBar)

    使用微信APP的小伙伴对于微信的ActionBar一定有印象,今天就带领大家一起实现以下这个效果. 第一步打开我们的开发工具,这里我使用的是Eclipse+ADT插件,然后创建我们的工程,这里选择Android的最低版本号为3.0或以上. 然后开始我们的"抄袭",首先打开我们微信,我们看到,顶部标题部分,分为左右两部分,左侧为"微信"两字,右侧则为搜索按钮+更多按钮,点击搜索按钮,会出现一个文本输入框.点击更多按钮,则会出现隐藏的menu菜单,分为:添加好友.发起群

随机推荐