结合上篇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. Sublime代码折叠

    一.显示折叠小三角 1.点击Sublime的Preferences->Setting-Default菜单,打开它的配置文件. 2.找到行 // Fold buttons are the tria ...

  2. 如何给DropDownList控件设置样式(ASP.NET MVC)

    前话: 应学校领导要求,要给后台管理系统添加一个搜索功能,提供可选择选项.我选择使用DropDownList去实现,熟悉.net控件的都知道,DropDownList的样子非常丑,不论是边框长宽还是里 ...

  3. Struts2框架具体解释

    在Struts2的Model-View-Controller模式实现下面五个核心组件: 动作-Actions 拦截器-Interceptors 值栈/OGNL 结果/结果类型 视图技术 Struts ...

  4. JDBC batch批处理Statement executeBatch 具体解释

    JDBC提供了数据库batch处理的能力,在数据大批量操作(新增.删除等)的情况下能够大幅度提升系统的性能.我曾经接触的一个项目,在没有採用batch处理时,删除5万条数据大概要半个小时左右,后来对系 ...

  5. Linux查看硬件信息以及驱动设备的命令

    用硬件检测程序kuduz探测新硬件:service kudzu start ( or restart) 查看CPU信息:cat /proc/cpuinfo 查看板卡信息:cat /proc/pci 查 ...

  6. linux中内核的一个不错的参数somaxconn

    导读:在linux中,/proc/sys/net/core/somaxconn这个参数,linux中内核的一个不错的参数somaxconn 看下其解析: 对于一个TCP连接,Server与Client ...

  7. java05 选择结构

    public static void main(String[] args) { /* * 张浩Java成绩大于90分,老师奖励他iPhone5 ,该怎么做呢? */ Scanner scanner= ...

  8. spring07 JDBC

    1.创建对应的数据库 2.在MyEclipse中创建项目  引入需要的jar包 3.创建数据访问层 public interface StudentDao { //新增学生 int addStuden ...

  9. ASP.NET-FineUI开发实践-4(二)

    在网上找了找,实验了一下window弹出和关闭的动画效果分享一下. 接上次的代码 default.js window_tips.animCollapse= true;//打开动画效果 window_t ...

  10. IE下判断IE版本的语句

    <!--[if lte IE 6]> <![endif]--> IE6及其以下版本可见   <!--[if lte IE 7]> <![endif]--> ...