昨天做项目的时候,需要弹出一个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()

否则的话,运行时会报错误


注:仅是个人解决的方法,仅供参考,勿扔板砖,谢谢

更多相关文章

  1. 浅谈Android五大布局(二)——RelativeLayout和TableLayout
  2. 笔记6
  3. 用Android自带的DrawerLayout和ActionBarDrawerToggle实现侧滑效
  4. 日积月累:配置SDK路径错误(An error occurred while automatically
  5. Android(安卓)Inflate方法
  6. Android(安卓)自定义RecyclerView 实现真正的Gallery效果
  7. Android(安卓)listview 实现列表多选
  8. Android(安卓)编译环境配置搭建(Ubuntu 12.04)及环境导致编译错
  9. Android偶遇问题处理

随机推荐

  1. Android(安卓)LinearLayout Why Gravity
  2. Android(安卓)Third Party Libraries and
  3. Android(安卓)Dev Intro - GLSurfaceView
  4. Android(安卓)Studio Gradle project syn
  5. Android(安卓)进度条
  6. Android中Handler机制
  7. Android(安卓)复制到剪贴板
  8. android xml中应用占位符
  9. Android(安卓)Camera2 API 学习笔记2
  10. Android(安卓)SMS(一) —— 读取短信