PopupWindow可实现弹出窗口效果。
PopupWindow与Dialog的区别是:
1.PopupWindow是阻塞式的,Dialog是非阻塞式的。
2.PopupWindow弹出时没有背景,Dialog弹出时有背景。
下面是一个简单的例子。
activity类:
package com.zzj.ui.popupwindow;
import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
import com.zzj.ui.R;
public class PopubWindowActivity extends Activity {
private PopupWindow popupWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popupwindow_activity);
initPopupwindow();
}
private void initPopupwindow() {
LayoutInflater inflater = LayoutInflater.from(this);
View popupwindowView = inflater
.inflate(R.layout.popupwindow_menu, null);
ListView listView = (ListView) popupwindowView
.findViewById(R.id.listView1);
listView.setAdapter(new ArrayAdapter<String>(this,
R.layout.popupwindow_menu_item,
new String[] { "复制", "修改", "删除" }));
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(PopubWindowActivity.this,
((TextView) view).getText(), Toast.LENGTH_LONG).show();
}
});
popupWindow = new PopupWindow();
popupWindow.setContentView(popupwindowView);
popupWindow.setFocusable(true);// 很重要
ColorDrawable background = new ColorDrawable(0x000000);
popupWindow.setBackgroundDrawable(background);// 很重要
popupWindow.setAnimationStyle(R.style.PopupwindowAnimation);// 弹出动画
popupWindow.setWidth(100);
popupWindow.setHeight(100);
}
public void popupwindow(View v) {
if (!popupWindow.isShowing()) {
popupWindow.showAsDropDown(v, 5, 5);
}
}
}
有两个非常重要的两个属性focusable和background需要设置。如果popupwindow没有得到焦点,则任何在popupwindow上的点击事件都无效。如果没有给popupwindow设置背景,当popupwindow获取焦点后,back键将无效,并且点击popupwindow之外的范围也无效;如果给popupwindow设置了背景,点击back键或者点击popupwindow之外的范围,popupwindow都将关闭。
activity布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="popupwindow"
android:text="popupwindow" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点得到我吗" />
</LinearLayout>
popupwindow布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
列表项(菜单项)布局文件:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/white" >
</TextView>
样式:
<!-- popupwindow弹出动画 -->
<style name="PopupwindowAnimation">
<item name="android:windowEnterAnimation">@anim/fade_in</item>
<item name="android:windowExitAnimation">@anim/fade_out</item>
</style>
anim/fade_in动画:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
android:duration="500"
android:fromXScale="0.0"
android:fromYScale="0.0"
android:toXScale="1.0"
android:toYScale="1.0" />
</set>
anim/fade_out动画:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
android:duration="500"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="0.0"
android:toYScale="0.0" />
</set>
效果: