1. 程式人生 > >android 自定義dialogfragment全屏對話窗體

android 自定義dialogfragment全屏對話窗體

public class ConfigDialogFragment extends DialogFragment implements View.OnClickListener{

    private ImageView force_close;
    private Dialog dialog;
    private Button ip_config;
    private EditText ip_txt;
    private SharedPreferences sp_ip;
    private SharedPreferences.Editor ip_edtior;
   private 
Button start_btn_store; private RadarScanView radarScanView;//雷達彈出檢視 private static Context context; private Toolbar toolbars; private TextView tvPrevTitle; private ImageView ivBack; @Override public void onClick(View v) { switch (v.getId()) { case R.id.start_btn_store
://返回重新配置 dialog.dismiss(); mListener.dialogDismiss();// radarScanView.setSearching(false); break; case R.id.ivBack: dialog.dismiss(); mListener.dialogDismiss();// radarScanView.setSearching(false); break;//返回鍵 default
: break; } } public interface DialogClickListener{ void doRadioWifi(); void doRadioScanDevice(); void dialogDismiss(); } static DialogClickListener mListener; public ConfigDialogFragment(){ } public static ConfigDialogFragment newInstance(Context context1,String title, String message, DialogClickListener listener){ ConfigDialogFragment frag = new ConfigDialogFragment(); Bundle b = new Bundle(); b.putString("title", title); b.putString("message", message); frag.setArguments(b); mListener = listener; context = context1; return frag; } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { dialog = new Dialog(getActivity(), R.style.DialogStyle); LayoutInflater inflater = (LayoutInflater) getActivity() .getSystemService(LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.config_dialog_fragment, null, false); //新增這一行 // LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.linear); // linearLayout.getBackground().setAlpha(255);//0~255透明度值 String title = getArguments().getString("title"); String message = getArguments().getString("message"); initView(view); initEvent(); //在這裡配置wifi dialog.setContentView(view); setCancelable(true);//這句話呼叫這個方法時,按對話方塊以外的地方不起作用。按返回鍵也不起作用 -setCancelable (false);按返回鍵也不起作用 StatusBarCompat.compat(getActivity(), getResources().getColor(R.color.colorPrimary));//更改標題欄的顏色 return dialog; } /** * dialogFragment鋪滿整個螢幕的好辦法 */ @Override public void onStart() { // TODO Auto-generated method stub super.onStart(); Window win = getDialog().getWindow(); // 一定要設定Background,如果不設定,window屬性設定無效 win.setBackgroundDrawable( new ColorDrawable(getResources().getColor(R.color.dialog_fragment_back))); DisplayMetrics dm = new DisplayMetrics(); getActivity().getWindowManager().getDefaultDisplay().getMetrics( dm ); android.view.WindowManager.LayoutParams params = win.getAttributes(); params.gravity = Gravity.BOTTOM; // 使用ViewGroup.LayoutParams,以便Dialog 寬度充滿整個螢幕 params.width = ViewGroup.LayoutParams.MATCH_PARENT; params.height = ViewGroup.LayoutParams.WRAP_CONTENT; win.setAttributes(params); } /** * 初始化存入伺服器地址的sp */ private void initSpIp() { sp_ip = getActivity().getSharedPreferences("IPInfo", MODE_PRIVATE); ip_edtior = sp_ip.edit(); } private void initView(View view) { tvPrevTitle = (TextView) view.findViewById(R.id.tvPrevTitle);//配對中 tvPrevTitle.setText("配對中"); ivBack = (ImageView)view.findViewById(R.id.ivBack);//返回鍵 start_btn_store = (Button)view.findViewById(R.id.start_btn_store);//返回重新配置 radarScanView = (RadarScanView) view.findViewById(R.id.radaScan); radarScanView.setSearching(true);//讓雷達檢視跑起來 } private void initEvent() { ivBack.setOnClickListener(this);//返回鍵 start_btn_store.setOnClickListener(this);//監聽雷達比如雷達掃到45s時,就可以掃裝置列表了 radarScanView.setOnDownActionListener(new OnDownActionListener() { @Override public void OnStart() { // TODO Auto-generated method stub // Toast.makeText( context, "雷達開始掃描", Toast.LENGTH_SHORT).show(); mListener.doRadioWifi(); } @Override public void OnEnd() { // TODO Auto-generated method stub // Toast.makeText( context, "雷達結束掃描", Toast.LENGTH_SHORT).show(); mListener.doRadioScanDevice();//當把ConfigDialogFragment給強制關閉時,就要把radaView給停掉 dialog.dismiss(); //快速連線完成後我要在這裡進行 ,裝置搜尋 } @Override public void onDismiss() { // TODO Auto-generated method stub dialog.dismiss();//80ms,後都沒能搜尋到裝置, mListener.dialogDismiss();// } }); }

}

activity中初始化配置:

//在這裡彈出dialogFragment對話方塊
if (!newFragment.isAdded()) {//DialogFragment.show()內部呼叫了FragmentTransaction.add()方法,所以呼叫DialogFragment.show()方法時候也可能
FragmentManager manager = getFragmentManager();
   FragmentTransaction ft = manager.beginTransaction();
   ft.add(newFragment, "dialog");
   ft.commit();
}
dialog樣式:
     <style name="DialogStyle">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:background">@android:color/background_light</item>
        <item name="android:windowFullscreen">true</item>
         <item name="android:windowBackground"> @android:color/transparent </item>
         <item name="android:backgroundDimEnabled">false</item>
</style>