如何将自定义布局设置为进度对话框背景?

How to set custom layout to progress dialog background?

标题中的问题)请帮助我) 我想从这个

那个

我试过实现它。

mProgressDialog = new ProgressDialog(this);
mProgressDialog.getWindow().setContentView(R.layout.footcloth);
mProgressDialog.setMessage(Constants.PROGRESS_DIALOG_MESSAGE);
mProgressDialog.show();

但是这段代码抛出异常

java.lang.RuntimeException: Unable to start activity ComponentInfo{im.anticafe.anticafeim/im.anticafe.anticafeim.activities.HomeActivity}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content

此代码在 activity BottomBarActivity

中实现
abstract public class BottomBarActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "bottomBarActivity";
    private int mWidth;
    private ProgressDialog mProgressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        mProgressDialog = new ProgressDialog(this);
        mProgressDialog.getWindow().setContentView(R.layout.footcloth);
        mProgressDialog.setMessage(Constants.PROGRESS_DIALOG_MESSAGE);
        mProgressDialog.show();
        ...
    }

    ...
}

而这个 activity 正在扩展到其他地方。 所以,请帮我解决我的问题) 谢谢)

有2个答案

  1. 如果你想在进度对话框下改变背景的颜色(透明度),我建议你使用片段并切换它们。在片段中,您可以设置任何属性并放置 ProgressBar

完成重新加载后,您可以再次切换回来,或切换到另一个片段

  1. 如果您想更改进度对话框

    public class TransparentProgressDialog extends Dialog {
    
      public TransparentProgressDialog(Context context) {
        super(context, R.style.TransparentProgressDialog);
    
        WindowManager.LayoutParams wlmp = getWindow().getAttributes();
    
        wlmp.gravity = Gravity.CENTER_HORIZONTAL;
        getWindow().setAttributes(wlmp);
        setTitle(null);
        setCancelable(false);
        setOnCancelListener(null);
        View view = LayoutInflater.from(context).inflate(
                R.layout.progress_dialog, null);
        setContentView(view);
       }
    }
    

在你的 activity 中使用

TransparentProgressDialog pd = new TransparentProgressDialog(context);
pd.show();

....

if (pd.isShowing()){
            pd.dismiss();
        }