自定义布局:
<?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="@drawable/bagra_shape"
android:orientation="vertical"
android:paddingLeft="40px">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="56px"
android:layout_marginBottom="46px"
android:text="更换头像"
android:textColor="@color/smssdk_black"
android:textSize="16sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="36px"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/minedialog2" />
<TextView
android:id="@+id/text2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="30px"
android:layout_gravity="center_vertical"
android:textSize="14sp"
android:textColor="#000"
android:text="从相册选择" />
</LinearLayout>
<LinearLayout
android:id="@+id/line2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="36px"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/minedialog1" />
<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_marginLeft="30px"
android:textSize="14sp"
android:textColor="#000"
android:text="拍照" />
</LinearLayout>
</LinearLayout>
实现类:
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.sxyuser.practice.R;
public class CustomPopupWindow extends PopupWindow implements View.OnClickListener {
private TextView btnTakePhoto, btnSelect;
private View mPopView;
private OnItemClickListener mListener;
public CustomPopupWindow(Context context) {
super(context);
init(context);
setPopupWindow();
btnTakePhoto.setOnClickListener(this);
btnSelect.setOnClickListener(this);
}
/**
* 初始化
*
* @param context
*/
private void init(Context context) {
LayoutInflater inflater = LayoutInflater.from(context);
//绑定布局
mPopView = inflater.inflate(R.layout.mine_head_bottom_dialog, null);
btnTakePhoto = (TextView) mPopView.findViewById(R.id.text1);
btnSelect = (TextView) mPopView.findViewById(R.id.text2);
}
/**
* 设置窗口的相关属性
*/
@SuppressLint("InlinedApi")
private void setPopupWindow() {
this.setContentView(mPopView);// 设置View
this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);// 设置弹出窗口的宽
this.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);// 设置弹出窗口的高
this.setFocusable(true);// 设置弹出窗口可
// this.setAnimationStyle(R.style.mypopwindow_anim_style);// 设置动画
this.setBackgroundDrawable(new ColorDrawable(0x00000000));// 设置背景透明
mPopView.setOnTouchListener(new View.OnTouchListener() {// 如果触摸位置在窗口外面则销毁
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
}
/**
* 定义一个接口,公布出去 在Activity中操作按钮的单击事件
*/
public interface OnItemClickListener {
void setOnItemClick(View v);
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.mListener = listener;
}
@Override
public void onClick(View v) {
if (mListener != null) {
mListener.setOnItemClick(v);
}
}
}
后面调用的时候直接初始化类:
//在另一个类中调用的时候需要继承接口:
implements CustomPopupWindow.OnItemClickListener;
private CustomPopupWindow mPop= new CustomPopupWindow(this);
mPop.setOnItemClickListener(this);
//设置PopupWindow中的位置
mPop.showAtLocation(MainCenterPersen.this.findViewById(R.id.main), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);