栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

PopupWindow仿微信浮层弹出框效果

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PopupWindow仿微信浮层弹出框效果

最近公司项目需要实现类似微信的浮层弹出框。研究发现是用PopupWindow实现的。而且可以自定义位置以及出现和退出时的动画,由于太晚了就不实现动画了,需要得同学请自己研究下。由于本人新手其中的不足和缺点请见谅。

代码如下:

首先是定义顶部按钮的main.xml文件

 
 
  
 
 

其次是定义弹出框PopupWindow的popupwindow_dialog.xml

 
 
  
  
 
 

接着是每一个弹出框显示的文字text.xml

 
 
 
  
 
 

最后是主界面的MainActivity.java

package com.bn.weixindemo; 
 
import android.app.Activity; 
import android.graphics.drawable.BitmapDrawable; 
import android.os.Bundle; 
import android.view.Gravity; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
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; 
import android.widget.AdapterView.OnItemClickListener; 
 
 
public class MainActivity extends Activity implements OnClickListener{ 
 private Button mBtnMore,mBtnAdd,mBtnSearch; 
 private PopupWindow popupWindow; 
 private LinearLayout layout; 
 private ListView listView; 
 private String[] more = {"我的相册","我的收藏","我的银行卡","设置","意见反馈"}; 
 private String[] add ={"发起群聊","添加朋友","视屏聊天","扫一扫","拍照分享"}; 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.main); 
 mBtnMore = (Button) findViewById(R.id.more); 
 mBtnAdd = (Button) findViewById(R.id.add); 
 mBtnSearch = (Button) findViewById(R.id.search); 
 setonClickListener(); 
 } 
 
 private void setonClickListener() { 
 mBtnMore.setonClickListener(this); 
 mBtnAdd.setonClickListener(this); 
 mBtnSearch.setonClickListener(this); 
 } 
 
 @Override 
 public void onClick(View v) { 
 // TODO Auto-generated method stub 
 switch (v.getId()) { 
 case R.id.more: 
  mBtnMore.getTop(); 
  int y = mBtnMore.getBottom() * 3 / 2; 
  int x = getWindowManager().getDefaultDisplay().getWidth(); 
  showMorePopupWindow(x, y); 
  break; 
 case R.id.add: 
  mBtnAdd.getTop(); 
  int y1 = mBtnAdd.getBottom() * 3 / 2; 
  int x1 = getWindowManager().getDefaultDisplay().getWidth(); 
  showAddPopupWindow(x1, y1); 
  break; 
 case R.id.search: 
  Toast.makeText(getbaseContext(), "搜索", 1).show(); 
 default: 
  break; 
 } 
 } 
 
 public void showMorePopupWindow(int x, int y) { 
 layout = (LinearLayout) LayoutInflater.from(MainActivity.this).inflate( 
  R.layout.popupwindow_dialog, null); 
 listView = (ListView) layout.findViewById(R.id.lv_dialog); 
 listView.setAdapter(new ArrayAdapter(MainActivity.this, 
  R.layout.text, R.id.tv_text, more)); 
 
 popupWindow = new PopupWindow(MainActivity.this); 
 popupWindow.setBackgroundDrawable(new BitmapDrawable()); 
 popupWindow 
  .setWidth(getWindowManager().getDefaultDisplay().getWidth() / 2); 
 popupWindow.setHeight(420); 
 popupWindow.setOutsideTouchable(true); 
 popupWindow.setFocusable(true); 
 popupWindow.setContentView(layout); 
 popupWindow.showAtLocation(findViewById(R.id.main), Gravity.LEFT 
  | Gravity.TOP, x, y);//需要指定Gravity,默认情况是center. 
 listView.setonItemClickListener(new onItemClickListener() { 
  @Override 
  public void onItemClick(AdapterView arg0, View arg1, int arg2, 
   long arg3) { 
  Toast.makeText(getbaseContext(), "您选择了:"+more[arg2], 1).show(); 
  popupWindow.dismiss(); 
  popupWindow = null; 
  } 
 }); 
 } 
  
 public void showAddPopupWindow(int x, int y) { 
 layout = (LinearLayout) LayoutInflater.from(MainActivity.this).inflate( 
  R.layout.popupwindow_dialog, null); 
 listView = (ListView) layout.findViewById(R.id.lv_dialog); 
 listView.setAdapter(new ArrayAdapter(MainActivity.this, 
  R.layout.text, R.id.tv_text, add)); 
 
 popupWindow = new PopupWindow(MainActivity.this); 
 popupWindow.setBackgroundDrawable(new BitmapDrawable()); 
 popupWindow 
  .setWidth(getWindowManager().getDefaultDisplay().getWidth() / 2); 
 popupWindow.setHeight(420); 
 popupWindow.setOutsideTouchable(true); 
 popupWindow.setFocusable(true); 
 popupWindow.setContentView(layout); 
 popupWindow.showAtLocation(findViewById(R.id.main), Gravity.LEFT 
  | Gravity.TOP, x, y);//需要指定Gravity,默认情况是center. 
 listView.setonItemClickListener(new onItemClickListener() { 
  @Override 
  public void onItemClick(AdapterView arg0, View arg1, int arg2, 
   long arg3) { 
  Toast.makeText(getbaseContext(), "您选择了:"+add[arg2], 1).show(); 
  popupWindow.dismiss(); 
  popupWindow = null; 
  } 
 }); 
 } 
} 

好了,主要代码就完成了,实现效果如下所示

由于本人没有图片,所以弹出框的背景图没有处理,弹出框中的每一项的前面也没有添加图片,有需要得同学可以自行添加。(效果已经出来了,细节没有调整,请大家见谅)

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/156353.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号