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


[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/ | 萝卜白菜的博客