android alertdialog布局 dialog布局
16lz
2021-01-26
昨天做项目的时候,需要弹出一个dialog,并且在在上面放一个view和两个button,于是搜资料android dialog上的布局,发现dialog 和 activity 的布局方法如出一辙,下面贴上代码:
AlertDialog:
AlertDialog dialog = new AlertDialog.Builder(BitmapOperatorActivity.this).create();
//Dialog 的话是 Dialog dialog = new Dialog(BitmapOperatorActivity.this);
dialog.show();dialog.setContentView(R.layout.alertdialog_layout);MyDialogView dialogView = (MyDialogView) dialog.findViewById(R.id.dialogview);Log.i("shuai", "dialog-->" + dialogView);dialogView.setBitmap(bitmap);
要特别注意的是,dialog.show(),一定要放在dialog.setContentView()的前面
否则的话会报下述错误:
android.util.AndroidRuntimeException: requestFeature() must be called before adding content
也就是必须先show,才能setContentView
如果要得到这个布局上的控件的话,就用dialog.findViewById()
特别声明:是dialog.findViewById()
否则的话,运行时会报错误
注:仅是个人解决的方法,仅供参考,勿扔板砖,谢谢
更多相关文章
- 浅谈Android五大布局(二)——RelativeLayout和TableLayout
- 笔记6
- 用Android自带的DrawerLayout和ActionBarDrawerToggle实现侧滑效
- 日积月累:配置SDK路径错误(An error occurred while automatically
- Android(安卓)Inflate方法
- Android(安卓)自定义RecyclerView 实现真正的Gallery效果
- Android(安卓)listview 实现列表多选
- Android(安卓)编译环境配置搭建(Ubuntu 12.04)及环境导致编译错
- Android偶遇问题处理