结合上篇android中使用ViewPager实现图片拖动,我们实现了点击“帮助”按钮的功能,这一篇则是接着上一篇,让我们一起来完成“我的”按钮的功能,这一功能,则是使用PopupWindow来实现弹出菜单。

再上项目结构图,如图:

从项目结构图可见,我们这里并没有新建新的Activity,因为“我的”按钮和“帮助”是在一个页面的,所以,我们只需新建一个效果图中的,弹出菜单的布局文件即可,即popup_menu.xml,代码如下:

Xml代码

<? xml   version = "1.0"   encoding = "utf-8" ?>
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:orientation = "vertical"
< span style = "background-color: #99cc00;" > android:background = "@drawable/popu_menu" </ span >
android:paddingLeft = "1dip"
android:paddingRight = "1dip"
android:paddingTop = "1dip"
android:paddingBottom = "14dip" >
< Button
android:id = "@+id/btn_my_favorites"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:gravity = "center"
android:text = "我的喜爱"
android:layout_weight = ""
android:textSize = "15sp"
android:textColor = "@android:color/white"
< span style = "background-color: #99cc00;" > android:background = "@drawable/button4" </ span >
android:textStyle = "bold" /> < View
android:layout_width = "fill_parent"
android:layout_height = "0.5dip"
android:background = "#eee" /> < Button
android:id = "@+id/btn_my_correction"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:layout_weight = ""
android:gravity = "center"
android:text = "我的收藏"
android:textColor = "@android:color/white"
android:textSize = "15sp"
android:textStyle = "bold" /> < View
android:layout_width = "fill_parent"
android:layout_height = "0.5dip"
android:background = "#eee" />
< Button
android:id = "@+id/btn_my_evaluation"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:gravity = "center"
android:layout_weight = ""
android:text = "我的评价"
android:textSize = "15sp"
android:textColor = "@android:color/white"
android:textStyle = "bold"
android:focusable = "true" />
</ LinearLayout >

上面标记为绿色的代码,即是我对弹出菜单的一些背景和按钮的修饰,否则原始的效果很难受。

@drawable/popu_menu ,popu_menu是一张图片,也就是弹出菜单的一张棕色的背景图,这个我就不上传附件了,大家随意换一张就行。

@drawable/button4 ,button4则是一个修饰原生button的xml文件,放置在drawable-hdpi这个文件夹,跟图片一起,代码如下:(颜色大家就自己配置吧,也就是我这里的android:drawable="XXXX",我就偷下懒了....)

button4.xml:

<? xml   version = "1.0"   encoding = "utf-8"   ?>
< selector xmlns:android = "http://schemas.android.com/apk/res/android" >
<!-- 没有焦点时的背景颜色 -->
< item android:state_window_focused = "false" android:drawable = "@color/buttonBg" />
<!-- 非触摸模式下获得焦点并单击时的背景颜色 -->
< item android:state_focused = "true" android:state_pressed = "true"
android:drawable = "@color/main_color" />
<!--触摸模式下单击时的背景颜色 -->
< item android:state_focused = "false" android:state_pressed = "true"
android:drawable = "@color/main2_color" />
<!--获得焦点时的背景 颜色-->
< item android:state_focused = "true" android:drawable = "@color/main2_color" />
</ selector >

最后,图片和布局都准备好了之后,就是在MainActivity.java中去编写代码了,具体流程则是先找到这个按钮,然后绑定事件,最后运行就可以了。代码如下:

Mainactivity.java:

package  com.test.citylist;  

import  android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupWindow; public class MainActivity extends Activity implements OnClickListener{ private Button btn_help,btn_menu;
private PopupWindow popupMenu; public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //注册控件并绑定事件
btn_help=(Button)findViewById(R.id.btn_help); //这个是上篇文章中的帮助按钮
btn_menu=(Button)findViewById(R.id.btn_my_menu); //这个是本篇的菜单按钮
btn_help.setOnClickListener( this );
btn_menu.setOnClickListener( this );
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true ;
} @Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_help:
Intent intent= new Intent();
intent.setClass(MainActivity. this , HelpActivity. class );
startActivity(intent);
break ; case R.id.btn_my_menu:
initPopupMenu(); //调用弹出菜单
break ;
default :
break ;
}
} //点击我的菜单
private void initPopupMenu(){
if (popupMenu== null ){
LayoutInflater lay = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = lay.inflate(R.layout.popup_menu, null );
//点击我的喜爱
((Button)v.findViewById(R.id.btn_my_favorites)).setOnClickListener( new OnClickListener(){
@Override
public void onClick(View view) {
popupMenu.dismiss();
Intent intent= new Intent(MainActivity. this ,MainActivity. class );
startActivity(intent);
}
});
//点击我的收藏
((Button)v.findViewById(R.id.btn_my_correction)).setOnClickListener( new OnClickListener(){
@Override
public void onClick(View view) {
popupMenu.dismiss();
Intent intent= new Intent(MainActivity. this ,MainActivity. class );
startActivity(intent);
}
});
//点击我的评价
((Button)v.findViewById(R.id.btn_my_evaluation)).setOnClickListener( new OnClickListener(){
@Override
public void onClick(View view) {
popupMenu.dismiss();
Intent intent= new Intent(MainActivity. this ,MainActivity. class );
startActivity(intent);
}
}); popupMenu = new PopupWindow(v, getApplicationContext().getResources().getDisplayMetrics().widthPixels/ ,
getApplicationContext().getResources().getDisplayMetrics().heightPixels/ , true );
}
//设置整个popupwindow的样式。
popupMenu.setBackgroundDrawable( new BitmapDrawable());
//使窗口里面的空间显示其相应的效果,比较点击button时背景颜色改变。
//如果为false点击相关的空间表面上没有反应,但事件是可以监听到的。
//listview的话就没有了作用。
popupMenu.setFocusable( true );
popupMenu.setOutsideTouchable( true );
popupMenu.update(); popupMenu.showAsDropDown(btn_menu);
}
}

到这里,运行代码,就可以看到效果图所示效果了,说做事要有始有终,既然我的这个页面上的两个按钮的功能已经分别实现了,那么最后一个“分享”按钮,我也不愿把它落下,那么点击“分享”后,是什么效果,用什么实现,请大家关注我的下一篇文章。

android中使用PopupWindow实现弹出窗口菜单的更多相关文章

  1. Android Demo---实现从底部弹出窗口

    在前面的博文中,小编简单的介绍了如何制作圆角的按钮以及圆角的图片,伴着键盘和手指之间的舞步,迎来新的问题,不知道小伙伴有没有这样的经历,以App为例,点击头像的时候,会从底部弹出一个窗口,有从相册中选 ...

  2. 【Android】百度地图自定义弹出窗口

    我们使用百度地图的时候,点击地图上的Marker,会弹出一个该地点详细信息的窗口,如下左图所示,有时候,我们希望自己定义这个弹出窗口的内容,或者,干脆用自己的数据来构造这样的弹出窗口,但是,在百度地图 ...

  3. Android之用PopupWindow实现弹出listview形式菜单

    Android 4.0之前的菜单使用非常广泛,但是在android4.0之后,很少使用先前的菜单样式了.那如何实现下图的样式了? 我们简单模拟一下. (1)屏蔽系统弹出的菜单: 1.首先创建至少一个系 ...

  4. flex中创建弹出窗口,并传值

    在flex页面中首先创建一个弹出窗口,代码如下: <?xml version="1.0" encoding="utf-8"?> <s:Titl ...

  5. OAF_开发系列08_实现OAF通过Popup参数式弹出窗口(案例)

    20150711 Created By BaoXinjian

  6. JS弹出窗口代码大全(详细整理)

    1.弹启一个全屏窗口 复制代码代码如下: <html> <body http://www.jb51.net','脚本之家','fullscreen');">; < ...

  7. asp.net弹出窗口并返回值

    a.html <form name="form1" method="post" action=""> <a href=&q ...

  8. Response.write()弹出窗口的问题!

    今天偶然发现在.NET中使用Javascript语句弹出窗口时发现一个小小的问题! 例子如下: 1: Response.Write ("<script languge=javascri ...

  9. Android 使用PopupWindow实现弹出菜单

    在本文当中,我将会与大家分享一个封装了PopupWindow实现弹出菜单的类,并说明它的实现与使用. 因对界面的需求,android原生的弹出菜单已不能满足我们的需求,自定义菜单成了我们的唯一选择,在 ...

随机推荐

  1. Windows 10 TH2

    Windows 10 TH2到底更新了啥? 15年11月,微软正式向Windows 10用户推送了Threshold 2(简称TH2)更新, 也就是传说中的November Update.更新后系统版 ...

  2. [Javascript] Advanced Reduce: Common Mistakes

    Take away: Always check you ruturn the accumulator Always pass in the inital value var data = [" ...

  3. C#获取窗口,模拟按键操作

    C#获取窗口,模拟按键操作,实现计算器模拟操作.首先引用. using System.Runtime.InteropServices; 使用DllImport引入两个函数: // Get a hand ...

  4. Java基础知识强化94:Calendar类之Calendar概述和获取日历字段的方法

    1. Calendar类概述:       Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR.MONTH.DAY_OF_MONTH.HOUR 等 日历字段之间的转换提供了一些方法,并 ...

  5. C语言原码反码补码与位运算.

      目录:     一.机器数和真值     二.原码,反码和补码的基础概念     三.为什么要使用原码,反码和补码     四.原码,补码,反码再深入     五.数据溢出测试     六.位运算 ...

  6. webform repeater

    repeater:由模板构成,解析后模板就不存在了             需要指定数据源进行数据绑定 List<Fruit> list = new FruitDA().Select(); ...

  7. adb服务无法启动

    今天学习android编程发现调试出错 The connection to adb is down, and a severe error has occured. You must restart ...

  8. How to Install Tomcat

    Read:http://www.ntu.edu.sg/home/ehchua/programming/howto/Tomcat_HowTo.html

  9. HDU_2068_RPG错排

    Problem Description 今年暑假杭电ACM集训队第一次组成女生队,其中有一队叫RPG,但做为集训队成员之一的野骆驼竟然不知道RPG三个人具体是谁谁.RPG给他机会让他猜猜,第一次猜:R ...

  10. 使用UILocalNotification给App添加本地消息通知

    使用过的代码,直接贴上 UILocalNotification *notification = [[UILocalNotification alloc] init]; if (notification ...