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

本文详细介绍了在Android中使用PopupWindow实现弹出菜单的方法,包括如何创建、显示及隐藏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.                 
  14.                 android:layout_alignParentBottom="true"
  15.                 android:layout_alignParentLeft="true"></Button>
  16.                 
  17.                 
  18.         <Button android:id="@+id/com_example_menueffect_effect1_btnCancel"
  19.         android:layout_width="wrap_content" 
  20.         android:layout_height="wrap_content"
  21.         android:text="取消"
  22.         
  23.                 android:layout_alignParentBottom="true"
  24.                 android:layout_alignParentRight="true"
  25.                 ></Button>
  26.                         
  27. </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.     
  14. </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.                                 
  40.                                 m_popupWindow=new PopupWindow(view,100, 200);
  41.                                 m_popupWindow.showAsDropDown(m_btnOk,0,m_btnOk.getHeight());
  42.                         
  43.                                 
  44.                         }
  45.                 });
  46.         }
  47. }

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


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

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

1、PopupWindow的隐藏

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

2、PopupWindow的显示及位置设置

1
window. showAtLocation (parent, Gravity. RIGHT  | Gravity. BOTTOM10, 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/ | 萝卜白菜的博客


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值