• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
百分百
http://www.qrw100.com
博客园    首页    新随笔    联系   管理    订阅  订阅

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. }
复制代码
posted @ 2011-11-01 11:36  爱尚美  阅读(225)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3