Android AlertDialog以及Dialog设置视图时区别

今天编写ProgressDialog对话框时创建窗体时出现以下错误:


错误代码如下:

	public void showProgressDialog(String message,boolean isCancleable){
		if(mProgressDialog==null){
			mProgressDialog=new ProgressDialog(mContext);
			mView_progress=LayoutInflater.from(this).inflate(R.layout.progress_dialog, null);
			mTextView_Content=(TextView) mView_progress.findViewById(R.id.progress_dialog_content);
			WindowManager.LayoutParams mWindowManager=getWindow().getAttributes();
			mWindowManager.height = ViewGroup.LayoutParams.MATCH_PARENT;
			mWindowManager.width  = ViewGroup.LayoutParams.MATCH_PARENT;
	        getWindow().setAttributes(mWindowManager);
			mTextView_Content.setText(message);
			mProgressDialog.setCancelable(isCancleable);
		}else{
			mTextView_Content.setText(message);
			mProgressDialog.setCancelable(isCancleable);
		}
		mProgressDialog.show();
		mProgressDialog.setContentView(mView_progress);
		
	}


由于ProgressDialog继承于AlertDialog,查找网上资料发现错误是由于在AlertDialog onCreate()方法中请求设置 mWindow.requestFeature(Window.FEATURE_NO_TITLE);

倘若再设置ContentView()时便会造成如上错误。在SetContentView之前调用show()方法便能避免如上的错误。附上AlertDialog中调用 mAlert.installContent();方法中的部分源代码:

    public void installContent() {
        /* We use a custom title so never request a window title */
        mWindow.requestFeature(Window.FEATURE_NO_TITLE);
        
        if (mView == null) {
            mWindow.setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
                    WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        }
        mWindow.setContentView(com.android.internal.R.layout.alert_dialog);
        setupView();
    }
    
    public void setTitle(CharSequence title) {
        mTitle = title;
        if (mTitleView != null) {
            mTitleView.setText(title);
        }
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值