上面图片中的确定按钮的效果。
也就是弹出菜单。
主activity布局
- <?xml version="1.0" encoding="utf-8"?>
-
-
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/com_example_menueffect_effect1_relativeLayout"
- android:layout_height="match_parent"
- android:layout_width="match_parent">
-
-
- <ListView android:layout_height="wrap_content" android:id="@+id/com_example_menueffect_effect1_listView1"
- android:layout_alignTop="@id/com_example_menueffect_effect1_relativeLayout"
- android:layout_width="match_parent"></ListView>
-
- <Button android:id="@+id/com_example_menueffect_effect1_btnOk"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="确定"
-
- android:layout_alignParentBottom="true"
- android:layout_alignParentLeft="true"></Button>
-
-
- <Button android:id="@+id/com_example_menueffect_effect1_btnCancel"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="取消"
-
- android:layout_alignParentBottom="true"
- android:layout_alignParentRight="true"
- ></Button>
-
- </RelativeLayout>
-
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <Button android:text="打开"
- android:id="@+id/com_example_menueffect_effect1_popupwindow_btnOpen" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
- <Button android:text="保存"
- android:id="@+id/com_example_menueffect_effect1_popupwindow_btnSave" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
- <Button android:text="退出"
- android:id="@+id/com_example_menueffect_effect1_popupwindow_btnExit" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
-
- </LinearLayout>
- package com.example.menueffect;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.ListView;
- import android.widget.PopupWindow;
-
- public class Effect1Activity extends Activity {
- Button m_btnOk;
- Button m_btnCancel;
- ListView m_lv;
- String[] m_users;
- PopupWindow m_popupWindow;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(com.example.R.layout.com_example_menueffect_effect1);
-
- m_users = new String[] { "ssssss", "bbbbbbb", "sfdsdfsdfsfd" };
-
- m_btnOk = (Button) findViewById(com.example.R.id.com_example_menueffect_effect1_btnOk);
- m_btnCancel = (Button) findViewById(com.example.R.id.com_example_menueffect_effect1_btnCancel);
- m_lv = (ListView) findViewById(com.example.R.id.com_example_menueffect_effect1_listView1);
-
- ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
- android.R.layout.simple_list_item_1, m_users);
- m_lv.setAdapter(arrayAdapter);
-
- m_btnOk.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- LayoutInflater layoutInflater = getLayoutInflater();
- View view = layoutInflater
- .inflate(
- com.example.R.layout.com_example_menueffect_effect1_popupwindow,
- null);
-
- m_popupWindow=new PopupWindow(view,100, 200);
- m_popupWindow.showAsDropDown(m_btnOk,0,m_btnOk.getHeight());
-
-
- }
- });
- }
- }
------------------------------------------------------------------------------------------------------------------------------
更多内容,查看:
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像素。
第二个参数指定起始点为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/ | 萝卜白菜的博客