如图所示:
如果一个项目有很多页面很多地方用到PopupWindow样式一样 里面的内容不一样 那么就可以抽取出公共的样式 只需传入不同的内容(里面的文字),通过点击回调的方式去让调用者去实现功能,这样可以大大减少重复的代码。
首先PopupWindow xml 布局如下所示:
LinearLayout ll_add_textview 通过传入的数据调用addView 方法 动态添加TextView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="@+id/ll_add_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/dp_15"
android:layout_marginEnd="@dimen/dp_15"
android:background="@drawable/shape_corner_colorffffff"
android:orientation="vertical">
</LinearLayout>
<TextView
android:id="@+id/tv_back"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginStart="@dimen/dp_15"
android:layout_marginEnd="@dimen/dp_15"
android:layout_marginBottom="@dimen/dp_20"
android:layout_marginTop="@dimen/dp_8"
android:gravity="center"
android:textColor="@color/color333333"
android:textSize="@dimen/sp_12"
android:background="@drawable/shape_corner_colorffffff"
android:text="取消"
/>
</LinearLayout>
定义一个公共的PopWindow类实现通用 :
调用getPopWindow方法传入一个数组
这里定义一个数组(根据自己的需求修改数组里面的内容)
public static final String[] ContactsManageMore ={“新建日程”,“编辑联系人信息”,“新建商机”,“新建甲方项目”,“作废”};
得到实例化对象mPopWindow 调用pop_Up_Window_add()方法即可
public class PopWindow implements View.OnClickListener {
public static Activity activity ;
public static PopWindow mPopWindow ;
private LinearLayout ll_add_textview;
private static String [] data ;
private PopupWindow window;
public static PopWindow getPopWindow(Activity ctx,String [] d){
activity = ctx ;
data = d ;
if(mPopWindow == null){
mPopWindow = new PopWindow();
}
return mPopWindow ;
}
public void pop_Up_Window_add() {
window = new PopupWindow(activity.getApplicationContext());
window.setBackgroundDrawable(new BitmapDrawable());
View view = View.inflate(activity.getApplicationContext(), R.layout.pop_add_customer_management, null);
ll_add_textview = view.findViewById(R.id.ll_add_textview);
for(int i=0 ;i<data.length;i++){
View tv = View.inflate(activity.getApplicationContext(), R.layout.ppwd_add_textview, null);
TextView viewById = tv.findViewById(R.id.tv_item);
viewById.setText(data[i]);
if("作废".equals(data[i]) || "删除".equals(data[i])){
viewById.setTextColor(Color.RED);
}
tv.setOnClickListener(this);
ll_add_textview.addView(tv);
}
window.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
window.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
TextView tv_back = view.findViewById(R.id.tv_back);
tv_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
window.dismiss();
}
});
WindowManager.LayoutParams params = activity.getWindow().getAttributes();
params.alpha = 0.7f;
activity.getWindow().setAttributes(params);
//点击其他地方消失
window.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
lp.alpha = 1f;
activity.getWindow().setAttributes(lp);
}
});
window.setContentView(view);
window.setOutsideTouchable(true);
window.setFocusable(true);
window.showAtLocation(activity.getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
}
public void dismissWindow(){
window.dismiss();
}
@Override
public void onClick(View v) {
for(int i= 0; i<ll_add_textview.getChildCount();i++){
View tv = ll_add_textview.getChildAt(i);
if(v == tv){
popWindowClickListener.onPopWindow(data[i]);
}
}
}
public PopWindowClickListener popWindowClickListener ;
public void setPopWindowClickListener(PopWindowClickListener popWindowClickListener){
this.popWindowClickListener = popWindowClickListener;
}
public interface PopWindowClickListener {
void onPopWindow(String textName);
}
}
最后通过实现PopWindow 自定义接口实现点击回调onPopWindow(String textName);
@Override
public void onPopWindow(String textName) {
switch (textName){
case"新建日程":
// 这里面实现要做的功能
break;
}
来源:CSDN
作者:怪咖plus
链接:https://blog.csdn.net/weixin_44248366/article/details/103984104