Android-自定义PopupWindow
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/animation_layout_content"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:clickable="true"
- android:orientation="vertical" >
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@drawable/abc_ab_bottom_solid_dark_holo"
- android:padding="12dip" >
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:gravity="center"
- android:orientation="horizontal" >
- <ImageView
- android:layout_width="30dp"
- android:layout_height="30dp"
- android:src="@drawable/ic_launcher" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="10dip"
- android:text="任务系统"
- android:textColor="@color/lightgray"
- android:textSize="18sp" />
- </LinearLayout>
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:gravity="center"
- android:orientation="horizontal" >
- <Button
- android:id="@+id/btnSearch"
- android:layout_width="30dp"
- android:layout_height="30dp"
- android:layout_marginRight="20dip"
- android:background="@drawable/actionbar_search_icon"
- android:visibility="gone" />
- <Button
- android:id="@+id/btnAdd"
- android:layout_width="30dp"
- android:layout_height="30dp"
- android:layout_marginRight="20dip"
- android:background="@drawable/actionbar_add_icon" />
- <Button
- android:id="@+id/btnSet"
- android:layout_width="30dp"
- android:layout_height="30dp"
- android:background="@drawable/actionbar_more_icon" />
- </LinearLayout>
- </RelativeLayout>
- </LinearLayout>
/14_CustomPopupWindow/res/layout/add_popup_dialog.xml
- <pre code_snippet_id="341527" snippet_file_name="blog_20140512_2_2271724" name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center_horizontal"
- android:orientation="vertical" >
- <LinearLayout
- android:id="@+id/pop_layout2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:layout_alignParentTop="true"
- android:background="@drawable/abc_ab_bottom_solid_dark_holo"
- android:gravity="center_horizontal"
- android:orientation="vertical" >
- <LinearLayout
- android:id="@+id/add_task_layout"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- android:padding="8dp" >
- <ImageView
- android:layout_width="35dp"
- android:layout_height="35dp"
- android:scaleType="fitCenter"
- android:src="@drawable/ofm_add_icon" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_marginLeft="10dp"
- android:gravity="center"
- android:text="添加任务"
- android:textColor="@color/white"
- android:textSize="15dip" />
- </LinearLayout>
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="0.2dp"
- android:background="@color/black" />
- <LinearLayout
- android:id="@+id/team_member_layout"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- android:padding="8dp" >
- <ImageView
- android:layout_width="35dp"
- android:layout_height="35dp"
- android:scaleType="fitCenter"
- android:src="@drawable/ofm_profile_icon" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_marginLeft="10dp"
- android:gravity="center"
- android:text="团队成员"
- android:textColor="@color/white"
- android:textSize="15dip" />
- </LinearLayout>
- </LinearLayout>
- </RelativeLayout></pre><br><br>
- <?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:gravity="center_horizontal"
- android:orientation="vertical" >
- <LinearLayout
- android:id="@+id/pop_layout"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:layout_alignParentTop="true"
- android:background="@drawable/abc_ab_bottom_solid_dark_holo"
- android:gravity="center_horizontal"
- android:orientation="vertical" >
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- android:padding="8dp" >
- <ImageView
- android:layout_width="50dp"
- android:layout_height="50dp"
- android:src="@drawable/defalt_head" />
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="left|center_horizontal"
- android:orientation="vertical"
- android:padding="5dp" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="wwj"
- android:textColor="@color/white"
- android:textSize="15sp" />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="whatswwj"
- android:textColor="@color/green"
- android:textSize="15sp" />
- </LinearLayout>
- </LinearLayout>
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="0.2dp"
- android:background="@color/black" />
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- android:padding="8dp" >
- <ImageView
- android:layout_width="35dp"
- android:layout_height="35dp"
- android:scaleType="fitCenter"
- android:src="@drawable/ofm_photo_icon" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_marginLeft="10dp"
- android:gravity="center"
- android:text="我的相册"
- android:textColor="@color/white"
- android:textSize="15sp" />
- </LinearLayout>
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="0.2dp"
- android:background="@color/black" />
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- android:padding="8dp"
- android:visibility="gone" >
- <ImageView
- android:layout_width="35dp"
- android:layout_height="35dp"
- android:scaleType="fitCenter"
- android:src="@drawable/ofm_collect_icon" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_marginLeft="10dp"
- android:gravity="center"
- android:text="我的收藏"
- android:textColor="@color/white"
- android:textSize="15sp" />
- </LinearLayout>
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="0.2dp"
- android:background="@color/black" />
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- android:padding="8dp"
- android:visibility="gone" >
- <ImageView
- android:layout_width="35dp"
- android:layout_height="35dp"
- android:scaleType="fitCenter"
- android:src="@drawable/ofm_card_icon" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_marginLeft="10dp"
- android:gravity="center"
- android:text="我的银行卡"
- android:textColor="@color/white"
- android:textSize="15sp" />
- </LinearLayout>
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="0.2dp"
- android:background="@color/black"
- android:visibility="gone" />
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- android:padding="8dp" >
- <ImageView
- android:layout_width="35dp"
- android:layout_height="35dp"
- android:scaleType="fitCenter"
- android:src="@drawable/ofm_setting_icon" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_marginLeft="10dp"
- android:gravity="center"
- android:text="设置"
- android:textColor="@color/white"
- android:textSize="15sp" />
- </LinearLayout>
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="0.2dp"
- android:background="@color/black" />
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- android:padding="8dp" >
- <ImageView
- android:layout_width="35dp"
- android:layout_height="35dp"
- android:scaleType="fitCenter"
- android:src="@drawable/ofm_blacklist_icon" />
- <Button
- android:id="@+id/btn_cancel"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_marginLeft="10dp"
- android:background="@null"
- android:gravity="center"
- android:text="退出登录"
- android:textColor="@color/white"
- android:textSize="15sp" />
- </LinearLayout>
- </LinearLayout>
- </RelativeLayout>
以上分别是主页面和两个popupWindow布局
- package com.wwj.popupwindow;
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.drawable.ColorDrawable;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.ViewGroup.LayoutParams;
- import android.widget.LinearLayout;
- import android.widget.PopupWindow;
- /**
- * 自定义popupWindow
- *
- * @author wwj
- *
- *
- */
- public class AddPopWindow extends PopupWindow {
- private View conentView;
- public AddPopWindow(final Activity context) {
- LayoutInflater inflater = (LayoutInflater) context
- .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- conentView = inflater.inflate(R.layout.add_popup_dialog, null);
- int h = context.getWindowManager().getDefaultDisplay().getHeight();
- int w = context.getWindowManager().getDefaultDisplay().getWidth();
- // 设置SelectPicPopupWindow的View
- this.setContentView(conentView);
- // 设置SelectPicPopupWindow弹出窗体的宽
- this.setWidth(w / 2 + 50);
- // 设置SelectPicPopupWindow弹出窗体的高
- this.setHeight(LayoutParams.WRAP_CONTENT);
- // 设置SelectPicPopupWindow弹出窗体可点击
- this.setFocusable(true);
- this.setOutsideTouchable(true);
- // 刷新状态
- this.update();
- // 实例化一个ColorDrawable颜色为半透明
- ColorDrawable dw = new ColorDrawable(0000000000);
- // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ,设置其他控件变化等操作
- this.setBackgroundDrawable(dw);
- // mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
- // 设置SelectPicPopupWindow弹出窗体动画效果
- this.setAnimationStyle(R.style.AnimationPreview);
- LinearLayout addTaskLayout = (LinearLayout) conentView
- .findViewById(R.id.add_task_layout);
- LinearLayout teamMemberLayout = (LinearLayout) conentView
- .findViewById(R.id.team_member_layout);
- addTaskLayout.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- AddPopWindow.this.dismiss();
- }
- });
- teamMemberLayout.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- AddPopWindow.this.dismiss();
- }
- });
- }
- /**
- * 显示popupWindow
- *
- * @param parent
- */
- public void showPopupWindow(View parent) {
- if (!this.isShowing()) {
- // 以下拉方式显示popupwindow
- this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 18);
- } else {
- this.dismiss();
- }
- }
- }
- package com.wwj.popupwindow;
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.drawable.ColorDrawable;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup.LayoutParams;
- import android.widget.PopupWindow;
- public class MorePopWindow extends PopupWindow {
- private View conentView;
- public MorePopWindow(final Activity context) {
- LayoutInflater inflater = (LayoutInflater) context
- .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- conentView = inflater.inflate(R.layout.more_popup_dialog, null);
- int h = context.getWindowManager().getDefaultDisplay().getHeight();
- int w = context.getWindowManager().getDefaultDisplay().getWidth();
- // 设置SelectPicPopupWindow的View
- this.setContentView(conentView);
- // 设置SelectPicPopupWindow弹出窗体的宽
- this.setWidth(w / 2 + 50);
- // 设置SelectPicPopupWindow弹出窗体的高
- this.setHeight(LayoutParams.WRAP_CONTENT);
- // 设置SelectPicPopupWindow弹出窗体可点击
- this.setFocusable(true);
- this.setOutsideTouchable(true);
- // 刷新状态
- this.update();
- // 实例化一个ColorDrawable颜色为半透明
- ColorDrawable dw = new ColorDrawable(0000000000);
- // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ,设置其他控件变化等操作
- this.setBackgroundDrawable(dw);
- // mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
- // 设置SelectPicPopupWindow弹出窗体动画效果
- this.setAnimationStyle(R.style.AnimationPreview);
- }
- public void showPopupWindow(View parent) {
- if (!this.isShowing()) {
- this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 18);
- } else {
- this.dismiss();
- }
- }
- }
- <style name="AnimationPreview">
- <item name="android:windowEnterAnimation">@anim/fade_in</item>
- <item name="android:windowExitAnimation">@anim/fade_out</item>
- </style>
用到两个动画资源
- <?xml version="1.0" encoding="utf-8"?>
- <!-- 左上角扩大-->
- <scale xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/accelerate_decelerate_interpolator"
- android:fromXScale="0.001"
- android:toXScale="1.0"
- android:fromYScale="0.001"
- android:toYScale="1.0"
- android:pivotX="100%"
- android:pivotY="10%"
- android:duration="200" />
/14_CustomPopupWindow/res/anim/fade_out.xml
- <?xml version="1.0" encoding="utf-8"?>
- <!-- 左上角缩小 -->
- <scale xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/accelerate_decelerate_interpolator"
- android:fromXScale="1.0"
- android:toXScale="0.001"
- android:fromYScale="1.0"
- android:toYScale="0.001"
- android:pivotX="100%"
- android:pivotY="10%"
- android:duration="200" />
- package com.wwj.popupwindow;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class MainActivity extends Activity implements OnClickListener{
- private Button setButton;
- private Button addButton;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_swipe);
- setButton = (Button) findViewById(R.id.btnSet);
- addButton = (Button) findViewById(R.id.btnAdd);
- setButton.setOnClickListener(this);
- addButton.setOnClickListener(this);;
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.btnSet:
- MorePopWindow morePopWindow = new MorePopWindow(MainActivity.this);
- morePopWindow.showPopupWindow(setButton);
- break;
- case R.id.btnSearch:
- break;
- case R.id.btnAdd:
- AddPopWindow addPopWindow = new AddPopWindow(MainActivity.this);
- addPopWindow.showPopupWindow(addButton);
- break;
- default:
- break;
- }
- }
- }
以上是代码实现,具体可以下载源码参考。
Android-自定义PopupWindow的更多相关文章
- Android 自定义Popupwindow 注意事项,手机和平板的区别
首先自定义ppw是要继承Popupwindow 的 而要成功的显示出自定义的ppw就必须实现下面的三句代码 // 必要的三要素下面,不然popWind显示不出来 this.setContentView ...
- Android 自定义PopupWindow动画效果
public class RollActivity extends Activity { private View view; private Button btn; private PopupWin ...
- Android自定义PopupWindow显示在控件上方或者下方
记录学习之用 View view = mInflater.inflate(R.layout.layout_popupwindow, null); PopUpwindowLayout popUpwind ...
- Android笔记之自定义PopupWindow
效果图 popup_window_addition.xml <?xml version="1.0" encoding="utf-8"?> <L ...
- android 自定义通知栏
package com.example.mvp; import cn.ljuns.temperature.view.TemperatureView;import presenter.ILoginPre ...
- Android自定义spinner下拉框实现的实现
一:前言 本人参考博客:http://blog.csdn.net/jdsjlzx/article/details/41316417 最近在弄一个下拉框,发现Android自带的很难实现我的功能,于是去 ...
- Android 自定义spinner下拉框实现
一:前言本人参考博客:http://blog.csdn.net/jdsjlzx/article/details/41316417 最近在弄一个下拉框,发现Android自带的很难实现我的功能,于是去网 ...
- 自定义PopupWindow弹出框(带有动画)
使用PopupWindow来实现弹出框,并且带有动画效果 首先自定义PopupWindow public class LostPopupWindow extends PopupWindow { pub ...
- Android 自定义View修炼-如何打造Android自定义的下拉列表框控件
一.概述 Android中的有个原生的下拉列表控件Spinner,但是这个控件有时候不符合我们自己的要求, 比如有时候我们需要类似windows 或者web网页中常见的那种下拉列表控件,类似下图这样的 ...
- Android 使用PopupWindow实现弹出菜单
在本文当中,我将会与大家分享一个封装了PopupWindow实现弹出菜单的类,并说明它的实现与使用. 因对界面的需求,android原生的弹出菜单已不能满足我们的需求,自定义菜单成了我们的唯一选择,在 ...
随机推荐
- java07循环结构
public class WhileTest { // while循环结构 public static void main(String[] args) { System.out.println(&q ...
- 用win32API 实现TextBox水印特效
demo效果:
- (转)js获取url参数值
明天有空编辑下 今天做项目遇到js取得url地址问号后面的参数,找了下面的,用着非常好,项目是选项卡样式的,也就是一点击二级分类,底下的同样名字的背景变红(选项卡倍选中) http://www.cnb ...
- grep和正则表达式
正则表达式示例表字 符 意 义 示 例* 任意长度的字符串. a* 表示: 空字符串.aaaa.a…? 长度为0或者1的字符串. a? 表示: 空字符串和a.+ 长度为一个或者多个的字符串. a+表示 ...
- JavaScript 函数之 ------------------ 闭包
谈到闭包,人们常常会把匿名函数和闭包混淆在一起.闭包是指由权访问另一个函数作用域中的变量的函数.创建闭包的常见方式,就是在一个函数内部创建另一个函数,仍以前面的 createComparisonFun ...
- Vim模式
Vim是从vi发展出来的一个文本编辑器.代码补完.编译及错误跳转等方便编程的功能特别丰富,在程序员中被广泛使用.和Emacs并列成为类Unix系统用户最喜欢的编辑器. Vim的第一个版本由布莱姆 ...
- JSP中取COOKIE中指定值得方法【转载】
Cookie cookies[]=request.getCookies(); //读出用户硬盘上的Cookie,并将所有的Cookie放到一个cookie对象数组里面 Cookie sCookie=n ...
- 用javascript预加载图片、css、js的方法研究
预加载的好处可以让网页更快的呈现给用户,缺点就是可能会增加无用的请求(但图片.css.js这些静态文件可以被缓存),如果用户访问的页面里面的css.js.图片被预加载了,用户打开页面的速度会快很多,提 ...
- FCKEditor的用法(asp版)
1:下载FCKEditor 去www.baidu.com 搜索,我本来想上传的,但不知道怎么传.下载下来后解压到你网站的目录,最好就放在根目录下,文件夹名字就用FCKEditor:这里可以随便自己喜好 ...
- JDK TOMCAT MAVEN在myeclipse如何配置
对于没有基础的人来说,本工具务必放在D盘根目录下. 1配置环境变量, "我的电脑-->右键-->属性" 貌似是这样吧,我电脑桌面没"我的电脑"或&q ...