我们将使用 PopupWindow 类来创建弹出窗口。
我想提一提的是,我们希望弹出窗口附加到打开它的按钮上。例如,如果上面屏幕截图中的“显示弹出窗口”按钮位于屏幕中间,我们希望弹出窗口固定在按钮的位置。为此,首先我们应该获取按钮在屏幕上的“x”和“y”位置,并将它们传递给弹出窗口。然后我们将使用偏移量来正确对齐弹出窗口——向右一点,向下一点,这样它就不会与整个按钮重叠。
我想提到的另一个想法是,我们将为弹出窗口使用 9 个补丁背景图像,因此它看起来会更花哨。但当然你可以跳过它并放置你想要的任何背景,或者根本没有背景。
创建 layout/main.xml 文件并添加一个按钮:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#CCC"
android:orientation="vertical" >
<Button
android:id="@+id/show_popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Popup" />
</LinearLayout>
创建一个新的布局文件:layout/popup_layout.xml,它定义了弹出的布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:id="@+id/popup"
android:layout_height="wrap_content"
android:background="@drawable/popup_bg"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Popup"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a simple popup" />
<Button
android:id="@+id/close"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Close" />
</LinearLayout>
现在是最有趣的部分。打开 TestPopupActivity 并用下面的代码填充它。仔细阅读评论以了解发生了什么。
public class TestPopupActivity extends Activity {
//The "x" and "y" position of the "Show Button" on screen.
Point p;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn_show = (Button) findViewById(R.id.show_popup);
btn_show.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//Open popup window
if (p != null)
showPopup(TestPopupActivity.this, p);
}
});
}
// Get the x and y position after the button is draw on screen
// (It's important to note that we can't get the position in the onCreate(),
// because at that stage most probably the view isn't drawn yet, so it will return (0, 0))
@Override
public void onWindowFocusChanged(boolean hasFocus) {
int[] location = new int[2];
Button button = (Button) findViewById(R.id.show_popup);
// Get the x, y location and store it in the location[] array
// location[0] = x, location[1] = y.
button.getLocationOnScreen(location);
//Initialize the Point with x, and y positions
p = new Point();
p.x = location[0];
p.y = location[1];
}
// The method that displays the popup.
private void showPopup(final Activity context, Point p) {
int popupWidth = 200;
int popupHeight = 150;
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);
// Creating the PopupWindow
final PopupWindow popup = new PopupWindow(context);
popup.setContentView(layout);
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setFocusable(true);
// Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
int OFFSET_X = 30;
int OFFSET_Y = 30;
// Clear the default translucent background
popup.setBackgroundDrawable(new BitmapDrawable());
// Displaying the popup at the specified location, + offsets.
popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
// Getting a reference to Close button, and close the popup when clicked.
Button close = (Button) layout.findViewById(R.id.close);
close.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
popup.dismiss();
}
});
}
}