本文共 1586 字,大约阅读时间需要 5 分钟。
<?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="match_parent"
android:layout_height="wrap_content"
android:textSize="26sp"
android:text="触发事件1"
/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="26sp"
android:text="触发事件2"
/>
</LinearLayout>
private void showPopupWindow(View view){
View contentView = LayoutInflater.from(mContext).inflate(R.layout.changephoto, null);
// 设置按钮的点击事件
Button button1 = (Button) contentView.findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//触发事件1
}
});
Button button2 = (Button) contentView.findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//触发事件2
}
});
final PopupWindow popupWindow = new PopupWindow(contentView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
popupWindow.setTouchable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setOutsideTouchable(true);
// 设置好参数之后再show
popupWindow.showAsDropDown(view);
}
@Overridepublic void onClick(View v) { showPopupWindow(v);}
转载地址:http://arnnx.baihongyu.com/