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 {
/** Called when the activity is first created. */
private Button but_menu;
private Button open_id;
private Button save_id;
View contentView;
private PopupWindow m_popupWindow;
@Override
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) {
// TODO Auto-generated method stub
m_popupWindow.dismiss();
}
});
// m_popupWindow = new PopupWindow();
but_menu.setOnClickListener(new OnClickListener() {
@Override
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() {
@Override
public void onClick(View v) {
m_popupWindow.dismiss();
Toast.makeText(TestActivity.this, "打开被触发", Toast.LENGTH_SHORT)
.show();
}
});
save_id.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
m_popupWindow.dismiss();
Toast.makeText(TestActivity.this, "保存被触发", Toast.LENGTH_SHORT)
.show();
}
});
}
@Override
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);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/lin_main"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:paddingRight="120dip">
<LinearLayout android:background="@drawable/popup_search"
android:orientation="vertical" android:layout_height="wrap_content"
android:layout_width="wrap_content">
<Button android:layout_width="fill_parent" android:background="@drawable/goods_bg"
android:id="@+id/btn_popup_information" android:layout_height="wrap_content"
android:textColor="#000000" android:textSize="16sp" android:text="打开"
android:layout_weight="1" />
<Button android:id="@+id/btn_popup_quote" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="保存"
android:background="@drawable/goods_bg" android:layout_weight="1"
android:textColor="#000000" android:textSize="16sp"
android:layout_marginTop="5dip" />
<Button android:id="@+id/btn_popup_product"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="关于我们" android:background="@drawable/goods_bg"
android:layout_weight="1" android:textColor="#000000"
android:textSize="16sp" android:layout_marginTop="5dip" />
<Button android:id="@+id/btn_popup_buy" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="关闭"
android:background="@drawable/shop_bg" android:layout_weight="1"
android:textColor="#000000" android:textSize="16sp"
android:layout_marginTop="5dip" />
</LinearLayout>
</LinearLayout>
<?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>
<?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:pivotX="0.100000024%"
android:pivotY="0.0"
android:fromXScale="1.0"
android:toXScale="0.0"
android:fromYScale="1.0"
android:toYScale="0.0"
>
</scale>
</set>
<?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:pivotX="0.100000024%"
android:pivotY="0.0"
android:fromXScale="0.0"
android:toXScale="1.0"
android:fromYScale="0.0"
android:toYScale="1.0"
>
</scale>
</set>