- package com.test.ui;
- import android.app.Activity;
- import android.graphics.drawable.BitmapDrawable;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.view.KeyEvent;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.ViewGroup.LayoutParams;
- import android.widget.Button;
- import android.widget.PopupWindow;
- import android.widget.Toast;
- public class TestActivity extends Activity {
- private Button but_menu;
- private Button open_id;
- private Button save_id;
- View contentView;
- private PopupWindow m_popupWindow;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- init();
- setListener();
- }
- private void init() {
- contentView = getLayoutInflater().inflate(R.layout.popupmenu, null,
- true);
- but_menu = (Button) findViewById(R.id.but_menu);
- open_id = (Button) contentView.findViewById(R.id.btn_popup_information);
- save_id = (Button) contentView.findViewById(R.id.btn_popup_quote);
- // PopupWindow弹出的窗口显示的view,第二和第三参数:分别表示此弹出窗口的大小
- m_popupWindow = new PopupWindow(contentView, LayoutParams.FILL_PARENT,
- LayoutParams.WRAP_CONTENT, true);
- m_popupWindow.setBackgroundDrawable(new BitmapDrawable());// 有了这句才可以点击返回(撤销)按钮dismiss()popwindow
- m_popupWindow.setOutsideTouchable(true);
- m_popupWindow.setAnimationStyle(R.style.PopupAnimation);
- }
- private void setListener() {
- contentView.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- m_popupWindow.dismiss();
- }
- });
- // m_popupWindow = new PopupWindow();
- but_menu.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- try {
- if (m_popupWindow.isShowing()) {
- m_popupWindow.dismiss();
- }
- m_popupWindow.showAsDropDown(v);
- } catch (Exception e) {
- Toast.makeText(TestActivity.this, e.getMessage(),
- Toast.LENGTH_SHORT);
- }
- }
- });
- open_id.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- m_popupWindow.dismiss();
- Toast.makeText(TestActivity.this, "打开被触发", Toast.LENGTH_SHORT)
- .show();
- }
- });
- save_id.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- m_popupWindow.dismiss();
- Toast.makeText(TestActivity.this, "保存被触发", Toast.LENGTH_SHORT)
- .show();
- }
- });
- }
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- if (keyCode == KeyEvent.KEYCODE_BACK) {
- if (m_popupWindow != null && m_popupWindow.isShowing()) {
- m_popupWindow.dismiss();
- return true;
- }
- }
- return super.onKeyDown(keyCode, event);
- }
- }
- popupmenu.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/lin_main"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:paddingRight="120dip" >
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/popup_search"
- android:orientation="vertical" >
- <Button
- android:id="@+id/btn_popup_information"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:background="@drawable/goods_bg"
- android:text="打开"
- android:textColor="#000000"
- android:textSize="16sp" />
- <Button
- android:id="@+id/btn_popup_quote"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="5dip"
- android:layout_weight="1"
- android:background="@drawable/goods_bg"
- android:text="保存"
- android:textColor="#000000"
- android:textSize="16sp" />
- <Button
- android:id="@+id/btn_popup_product"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="5dip"
- android:layout_weight="1"
- android:background="@drawable/goods_bg"
- android:text="关于我们"
- android:textColor="#000000"
- android:textSize="16sp" />
- <Button
- android:id="@+id/btn_popup_buy"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="5dip"
- android:layout_weight="1"
- android:background="@drawable/shop_bg"
- android:text="关闭"
- android:textColor="#000000"
- android:textSize="16sp" />
- </LinearLayout>
- </LinearLayout>
- 下面的是popwindow出现和退出的动画特效,这种东西网上有很多,就拿出这个吧.
- values/style.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <style name="PopupAnimation" parent="android:Animation">
- <item name="android:windowEnterAnimation">@anim/popup_search_show</item>
- <item name="android:windowExitAnimation">@anim/popup_search_hide</item>
- </style>
- </resources>anim/popup_search_show.xml
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/decelerate_interpolator">
- <scale android:duration="500" android:fromXScale="1.0" android:fromYScale="1.0" android:pivotX="0.100000024%" android:pivotY="0.0" android:toXScale="0.0" android:toYScale="0.0"></scale>
- </set>
- anim/popup_search_hide.xml
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/decelerate_interpolator" >
- <scale
- android:duration="500"
- android:fromXScale="0.0"
- android:fromYScale="0.0"
- android:pivotX="0.100000024%"
- android:pivotY="0.0"
- android:toXScale="1.0"
- android:toYScale="1.0" >
- </scale>
- </set>