在android开发中使用dialogframent实现对话框是比较支持的方法,因为对屏幕旋转及回退都能有比较好的控制。
自定义的dialogfragment继承DialogFragment,并实现onCreateView()方法,如下:
public class MyDialogFragment extends DialogFragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_dialog, container,false); } }
在activity中调用:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); MyDialogFragment dialog = new MyDialogFragment(); dialog.show(ft, "test"); }