`
mmdev
  • 浏览: 12922825 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

[Android实例] popupwindow实现弹出菜单效果

 
阅读更多


上面图片中的确定按钮的效果。
也就是弹出菜单。
主activity布局
  1. <?xml version="1.0" encoding="utf-8"?>


  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/com_example_menueffect_effect1_relativeLayout"
  4. android:layout_height="match_parent"
  5. android:layout_width="match_parent">


  6. <ListView android:layout_height="wrap_content" android:id="@+id/com_example_menueffect_effect1_listView1"
  7. android:layout_alignTop="@id/com_example_menueffect_effect1_relativeLayout"
  8. android:layout_width="match_parent"></ListView>

  9. <Button android:id="@+id/com_example_menueffect_effect1_btnOk"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:text="确定"

  13. android:layout_alignParentBottom="true"
  14. android:layout_alignParentLeft="true"></Button>


  15. <Button android:id="@+id/com_example_menueffect_effect1_btnCancel"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:text="取消"

  19. android:layout_alignParentBottom="true"
  20. android:layout_alignParentRight="true"
  21. ></Button>

  22. </RelativeLayout>

复制代码
popupmenu布局
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:orientation="vertical"
  5. android:layout_width="wrap_content"
  6. android:layout_height="wrap_content">
  7. <Button android:text="打开"
  8. android:id="@+id/com_example_menueffect_effect1_popupwindow_btnOpen" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
  9. <Button android:text="保存"
  10. android:id="@+id/com_example_menueffect_effect1_popupwindow_btnSave" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
  11. <Button android:text="退出"
  12. android:id="@+id/com_example_menueffect_effect1_popupwindow_btnExit" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

  13. </LinearLayout>
复制代码
主activity代码
  1. package com.example.menueffect;

  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.Gravity;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.ArrayAdapter;
  9. import android.widget.Button;
  10. import android.widget.ListView;
  11. import android.widget.PopupWindow;

  12. public class Effect1Activity extends Activity {
  13. Button m_btnOk;
  14. Button m_btnCancel;
  15. ListView m_lv;
  16. String[] m_users;
  17. PopupWindow m_popupWindow;

  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. // TODO Auto-generated method stub
  21. super.onCreate(savedInstanceState);
  22. setContentView(com.example.R.layout.com_example_menueffect_effect1);

  23. m_users = new String[] { "ssssss", "bbbbbbb", "sfdsdfsdfsfd" };

  24. m_btnOk = (Button) findViewById(com.example.R.id.com_example_menueffect_effect1_btnOk);
  25. m_btnCancel = (Button) findViewById(com.example.R.id.com_example_menueffect_effect1_btnCancel);
  26. m_lv = (ListView) findViewById(com.example.R.id.com_example_menueffect_effect1_listView1);

  27. ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
  28. android.R.layout.simple_list_item_1, m_users);
  29. m_lv.setAdapter(arrayAdapter);

  30. m_btnOk.setOnClickListener(new OnClickListener() {

  31. @Override
  32. public void onClick(View v) {
  33. // TODO Auto-generated method stub
  34. LayoutInflater layoutInflater = getLayoutInflater();
  35. View view = layoutInflater
  36. .inflate(
  37. com.example.R.layout.com_example_menueffect_effect1_popupwindow,
  38. null);

  39. m_popupWindow=new PopupWindow(view,100, 200);
  40. m_popupWindow.showAsDropDown(m_btnOk,0,m_btnOk.getHeight());


  41. }
  42. });
  43. }
  44. }

------------------------------------------------------------------------------------------------------------------------------
更多内容,查看:


Android下PopupWindow隐藏及显示(showAtLocation/showAsDropDown)

上一篇对PopupWindow的用法(位置、动画、焦点)做了详细介绍,具体查看Android中PopupWindow的用法(位置、动画、焦点)。下面说说PopupWindow的如何隐藏、显示及显示位置(showAtLocation/showAsDropDown)。

1、PopupWindow的隐藏

1
2
3
4
finalPopupWindow window=mPageStatWin;
if(null!=window&&window.isShowing()){
win.dismiss();
}

2、PopupWindow的显示及位置设置

1
window.showAtLocation(parent, Gravity.RIGHT|Gravity.BOTTOM,10,10);
第一个参数指定PopupWindow的锚点view,即依附在哪个view上。
第二个参数指定起始点为parent的右下角,第三个参数设置以parent的右下角为原点,向左、上各偏移10像素。
1
2
3
4
//将PopupWindow作为anchor的下拉窗口显示。即在anchor的左下角显示
window.showAsDropDown(anchor);
//xoff,yoff基于anchor的左下角进行偏移。
window.showAsDropDown(anchor, xoff, yoff);
如果没有充足的空间显示PopupWindow,那么PopupWindow的左下角将位于anchor的左上角来显示。

转载请注明地址:http://orgcent.com/android-popupwindow-showasdropdown-showatlocation/ | 萝卜白菜的博客


分享到:
评论

相关推荐

    Android之用PopupWindow实现弹出菜单的方法详解

    在使用UC-WebBrowser时,你会发现它的弹出菜单跟系统自带的菜单不一样。它实现更多菜单选项的显示和分栏。其实,它的本身是PopupWindow或者是AlertDialog对话框,在里面添加两个GridView控件,一个是菜单标题栏,一...

    安卓PopupWindow弹出菜单实例

    androud实现PopupWindow弹出菜单简单实例

    Android PopupWindow实现屏幕底部菜单.rar

    Android PopupWindow实现屏幕底部...另外效果还包括了弹出的菜单,之前思路有两种,第一种,写布局实现,第二种,弹出PopupWindow实现,很明显,第二种效果要更好一些,本源码包就是使用PopupWindow来实现的悬浮菜单。

    Android 使用PopupWindow实现弹出更多的菜单实例详解

    最近想要做一个弹出更多的菜单,而原生的弹出菜单却不是我们想要的效果,所以必然要自定义菜单。接下来通过本文给大家介绍android 使用popupwindow实现弹出更多的菜单实例详解,需要的朋友可以参考下

    一个类似UC右上角的弹出菜单(使用PopupWindow)

    一个类似UC右上角的弹出菜单(使用PopupWindow)实例

    Android使用Activity实现从底部弹出菜单或窗口的方法

    本文实例讲述了Android使用Activity实现从底部弹出菜单或窗口的方法。分享给大家供大家参考,具体如下: 这里使用activity实现弹出滑动窗口或菜单,主要是使用了一些设置activity的样式来实现弹出窗口和滑动效果,...

    简单实现Android弹出菜单效果

    本文实例为大家分享了Android弹出菜单效果的具体代码,供大家参考,具体内容如下 功能描述:用户单击按钮弹出菜单。当用户选择一个菜单项,会触发MenuItemClick事件并让弹出的菜单消失;如果用户在菜单外单击,则...

    Android编程之ICS式下拉菜单PopupWindow实现方法详解(附源码下载)

    这种菜单实际上就是一个弹出式的菜单,于是我们想到android PopupWindow 类,给他设置一个view 在弹出来不就OK了吗。 PopupWindow 的用法也很简单 主要方法: 步骤1.new 一个实例出来,我们使用这个构造方法即可, ...

    Android自定义popupwindow实例代码

    主要为大家详细介绍了Android自定义popupwindow实例代码,popupwindow弹出菜单效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

    android PopupWindow 和 Activity弹出窗口实现方式

    目前只见过两种弹出框的实现方式,第一种是最常见的PopupWindow,第二种也就是Activity的方式是前几天才见识过。感觉很霸气哦。没想到,activity也可以做伪窗口。 先贴上最常见的方法,主要讲activity的方法。 一、...

    PopWindow示例(转载)

    Android用PopupWindow实现弹出菜单实例----来源------&gt;&gt;&gt;&gt; 作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!http://blog.csdn.net/ouyang_peng

    Android 自定义弹出菜单和对话框功能实例代码

    Android 开发当中,可能会存在许多自定义布局的需求,比如自定义弹出菜单(popupWindow),以及自定义对话框(Dialog)。 话不多说,直接上图片。   先讲第一种,自定义PopUpWindow 1.popupWindow protected ...

    android仿新闻阅读器菜单弹出效果实例(附源码DEMO下载)

    下面的菜单弹出效果在很多的新闻阅读器上都有,比如今日头条、360新闻等。 其实这个实现起来很简单,看其效果,其实就是一个PopupWindow,之后设定相应postion的按钮点击属性,之后获取按钮的位置,给它设置动画...

    Android组件实现长按弹出上下文菜单功能的方法

    本文实例讲述了Android组件实现长按弹出上下文菜单功能的方法。分享给大家供大家参考,具体如下: 简单组件长按弹出上下文菜单总结 第一步:在程序合适位置给一个控件注册上下文菜单 组件可以是按钮,文本框,还可以...

    Android仿QQ长按弹出删除复制框

    一般会采用长按弹出菜单的形式,默认的上下文菜单比较难看,而QQ的上下文菜单就人性化多了,整个菜单给用户一种气泡弹出的感觉,而且会显示在手指按下的位置,而技术实现我之前是使用popupWindow和RecyclerView实现...

    Android编程实现仿易信精美弹出框效果【附demo源码下载】

    本文实例讲述了Android编程实现仿易信精美弹出框效果。分享给大家供大家参考,具体如下: 截图: 动画效果介绍: 1.点击ActionBar上“+”按钮,菜单从上方弹出(带反弹效果); 2.再次点击“+”、点击空白区域或者...

    Android中PopupMenu组件的使用实例

    要求弹出的PopupWindow里面是一个列表,我们使用时都是在里面套个ListView或RecyclerView ,现在我们不需要在做这样繁琐的工作了。 在官方android.support.v7.widget 包下提供的 PopupMenu 组件,已经被越来越多的...

Global site tag (gtag.js) - Google Analytics