Android自定义对话框 还是会出现背景框

Android 码拜 8年前 (2016-05-10) 912次浏览
请大牛指点
Android 自定义的进度条对话框,用AlertDialog实现,本人写一个测试程序,效果很正常,图片如下
http://fhr.luckcome.com/temp/01.png
但是将它移植到本人的项目后,对话框的宽度就不是设定的 match_parent 了,而且还多了一个背景框,不知道这个背景框是哪来的,都是自定义的,测试代码怎么就是正常的
http://fhr.luckcome.com/temp/02.png
下面是进度条对话框布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/dialog_bg1"
    android:orientation="vertical" >
    
    <TextView 
        android:id="@+id/progressTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#0000ff"
        android:text=""
        />
    
    <ProgressBar 
       android:id="@+id/progressBar"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_marginLeft="10dp"
       android:layout_marginRight="10dp"
       android:layout_marginBottom="10dp"
       style="?android:attr/progressBarStyleHorizontal"
       android:progressDrawable="@drawable/my_progress_bar"
       android:max="100"
       android:visibility="visible" />
</LinearLayout>

下面是自定义的对话框类

package com.example.dialog20141118;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MyProgressDialog {

	private Context context;
	private AlertDialog mDialog;
	private View mView;
	private LayoutInflater mInflater;
	private AlertDialog.Builder mBuilder;

	private TextView mTextView;
	private String title;

	private ProgressBar mProgressBar;
	private int mProgress=0;
	private int mSecondaryProgress=0;
	private int mMax=100;
	private int mVisibility=0;

	/**
	 * 构造函数
	 * @param context
	 */
	@SuppressLint("InflateParams")
	public MyProgressDialog(Context context,String title) {
		this.context = context;
		this.title = title;

		mInflater = LayoutInflater.from(context);
		mView = mInflater.inflate(R.layout.my_prgdlg_layout, null);

		mBuilder = new AlertDialog.Builder(context);
		mBuilder.setView(mView);

		mDialog = mBuilder.create();
		mDialog.setCanceledOnTouchOutside(false);

		mProgressBar = (ProgressBar)mView.findViewById(R.id.progressBar);
		mTextView = (TextView) mView.findViewById(R.id.progressTitle);
		mTextView.setText(title);

	}

	/**
	 * 显示对话框
	 */
	public void show(){
		mDialog.show();
	}

	/**
	 * 销毁对话框
	 */
	public void dismiss() {
		mDialog.dismiss();
	}

	/**
	 * 进度条显示进程
	 */
	Thread progressRun = new Thread() {
		@Override
		public void run() {
			while(true) {
				mProgressBar.setProgress(mProgress);
			}
		}

	};

	/**
	 * 测试进程,配合 progressRun 进程使用
	 */
	Thread progressCount = new Thread() {

		@Override
		public void run() {
			while(mProgress < 100) {
				mProgress++;
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			dismiss();
		}
	};

	/**
	 * 启动进度条显示刷新进程
	 */
	void progressStrart() {
		progressRun.start();
		// 测试用,使进度条跑动
		progressCount.start();
	}

	/**
	 * 获取对话框标题内容
	 * @return
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * 设置对话框标题
	 * @param title
	 */
	public void setTitle(String title) {
		this.title = title;
	}
	/**
	 * 获取 进度条进度值
	 * @return
	 */
	public int getmProgress() {
		return mProgress;
	}
	/**
	 * 设置进度条进度值
	 * @param mProgress
	 */
	public void setmProgress(int mProgress) {
		this.mProgress = mProgress;
	}
	/**
	 * 获取第二进度的进度值
	 * @return
	 */
	public int getmSecondaryProgress() {
		return mSecondaryProgress;
	}
	/**
	 * 设置第二进度的进度值
	 * @param mSecondaryProgress
	 */
	public void setmSecondaryProgress(int mSecondaryProgress) {
		this.mSecondaryProgress = mSecondaryProgress;
	}
	/**
	 * 获取进度的满量程值
	 * @return
	 */
	public int getmMax() {
		return mMax;
	}
	/**
	 * 设置进度的满量程值
	 * @param mMax
	 */
	public void setmMax(int mMax) {
		this.mMax = mMax;
	}

}
解决方案

5

你继承AlertDialog 在onCreate方法中setContentView试下

5

要设置style
View diaView=View.inflate(this, R.layout.dialog, null);
dialog=new Dialog(AuthorizeActivity.this,R.style.dialog);
dialog.setContentView(diaView);
dialog.show();

5

添加个style:
<resources>
<style name=”dialog” parent=”@android:style/Theme.Dialog”>
<item name=”android:windowBackground”> @android:color/transparent </item>
</style>
</resources>
然后改一下这里的代码:
mBuilder = new AlertDialog.Builder(context,R.style.dialog);

5

mBuilder.setView(mView,0,0,0,0);   用这个参数的 去填充对话框

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Android自定义对话框 还是会出现背景框
喜欢 (0)
[1034331897@qq.com]
分享 (0)