android 彈窗黑邊規避,程式碼自定義佈局不用
阿新 • • 發佈:2019-01-01
採用popwindow實現無黑邊彈窗
程式碼:
public class ExitGameDialog{ Context context; RelativeLayout contentViewbg; RelativeLayout contentView; RelativeLayout.LayoutParams content_parms; android.view.ViewGroup.LayoutParams alert_layout_parms; String orgen="#fe9d00"; String green="#5ec324"; PopupWindow pop; public ExitGameDialog(Context context,ExitGameCallBack mBack1) { //super(context); this.context = context; this.mBack=mBack1; WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); int screenwidth = manager.getDefaultDisplay().getWidth(); int screenHeight = manager.getDefaultDisplay().getHeight(); int leftAndRight=DisplayUtil.dip2px(context,25); //xml中佈局直接View view = inflater.inflate(R.layout.test_alertdialog_listview,null);引入即可 //<!------------------------程式碼設定佈局,xml佈局可以直接跳過換用inflate引入-------------------------------> //全屏透明背景 contentViewbg=new RelativeLayout(context); alert_layout_parms=new LayoutParams(android.view.ViewGroup.LayoutParams.MATCH_PARENT,android.view.ViewGroup.LayoutParams.MATCH_PARENT); contentViewbg.setBackgroundColor(Color.parseColor("#ba000000")); contentViewbg.setGravity(Gravity.CENTER); contentView=new RelativeLayout(context); content_parms=new RelativeLayout.LayoutParams(screenwidth-2*leftAndRight,RelativeLayout.LayoutParams.WRAP_CONTENT); contentView.setBackgroundColor(Color.WHITE); contentView.setGravity(Gravity.CENTER); contentView.setPadding(leftAndRight,leftAndRight,leftAndRight,leftAndRight); LinearLayout content_layout=new LinearLayout(context); RelativeLayout.LayoutParams content_layout_parms=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT); content_layout.setOrientation(LinearLayout.VERTICAL); TextView tv_title=new TextView(context); LinearLayout.LayoutParams tv_title_parms=new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT); tv_title_parms.gravity=Gravity.CENTER_HORIZONTAL; tv_title.setText("溫馨提示"); tv_title.setTextColor(Color.parseColor(orgen)); tv_title.setTextSize(17); TextView tv_fgx=new TextView(context); int fgxh=DisplayUtil.dip2px(context,0.5f); LinearLayout.LayoutParams tv_fgx_parms=new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,fgxh); tv_fgx_parms.topMargin=DisplayUtil.dip2px(context,15); tv_fgx.setBackgroundColor(Color.GRAY); TextView tv_content=new TextView(context); LinearLayout.LayoutParams tv_content_parms=new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT); tv_content_parms.topMargin=DisplayUtil.dip2px(context,25); tv_content.setText("確認退出遊戲嗎?"); tv_content.setTextColor(Color.parseColor("#333333")); tv_content.setTextSize(15); LinearLayout ll_btn_layout=new LinearLayout(context); ll_btn_layout.setOrientation(LinearLayout.HORIZONTAL); LinearLayout.LayoutParams ll_btn_layout_parms=new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT); ll_btn_layout_parms.topMargin=DisplayUtil.dip2px(context,25); int btn_top=DisplayUtil.dip2px(context,6); TextView tv_ok=new TextView(context); LinearLayout.LayoutParams tv_ok_parms=new LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT); tv_ok.setGravity(Gravity.CENTER); tv_ok.setPadding(0, btn_top, 0, btn_top); tv_ok_parms.weight=1; tv_ok.setText("確定"); tv_ok.setTextColor(Color.WHITE); tv_ok.setBackgroundColor(Color.parseColor(orgen)); tv_ok.setTextSize(15); tv_ok.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { pop.dismiss(); Log.e("test","click ok btn"); } }); TextView tv_cancel=new TextView(context); LinearLayout.LayoutParams tv_cancel_parms=new LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT); tv_cancel_parms.weight=1; tv_cancel_parms.leftMargin=DisplayUtil.dip2px(context,20); tv_cancel.setPadding(0, btn_top, 0, btn_top); tv_cancel.setGravity(Gravity.CENTER); tv_cancel.setText("返回"); tv_cancel.setTextColor(Color.WHITE); tv_cancel.setTextSize(15); tv_cancel.setBackgroundColor(Color.parseColor(green)); tv_cancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { pop.dismiss(); Log.e("test","click goback btn"); } }); ll_btn_layout.addView(tv_ok, tv_ok_parms); ll_btn_layout.addView(tv_cancel, tv_cancel_parms); content_layout.addView(tv_title,tv_title_parms); content_layout.addView(tv_fgx,tv_fgx_parms); content_layout.addView(tv_content,tv_content_parms); content_layout.addView(ll_btn_layout,ll_btn_layout_parms); contentView.addView(content_layout,content_layout_parms); contentViewbg.addView(contentView,content_parms); pop=new PopupWindow(contentViewbg, screenwidth, screenHeight); pop.showAtLocation(contentViewbg,Gravity.CENTER, 0, 0); } }