进度对话框setContentView无法在onCreateDialog DialogFragment中使用

时间:2018-05-25 10:27:41

标签: android progressdialog android-dialogfragment android-progressbar

我正在使用此代码根据我的需要显示Progressbar

public class DelayedProgressDialog extends  DialogFragment
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
         setCancelable(false);
    }



    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
          ProgressDialog dialog = new ProgressDialog(getActivity());
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        dialog.setTitle(null);
        dialog.setContentView(R.layout._task);
        dialog.setIndeterminate(false);
        return dialog;
    }
}

的xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     >

    <ProgressBar
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="@dimen/_100sdp"
        android:layout_height="@dimen/_100sdp"
        android:id="@+id/progressBar"
        android:layout_centerInParent="true"
        android:indeterminateTint="@android:color/white"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

现在的问题是,当我调用此代码时,我的自定义进度条未显示在此片段中: 在没有标题的左侧显示正常进度条,我想在中心显示。如果我删除了setContentview然后没有得到任何效果,这意味着我的自定义视图没有在此显示,我怎样才能在其中显示我的视图。

1 个答案:

答案 0 :(得分:1)

您可以使用基本对话框,例如Dialog类,然后设置您在布局中设计的内容视图(这似乎是正确的)。由于您使用的是ProgressDialog,因此可能会干扰您在布局中定义的ProgressBar窗口小部件。

以下是如何做到这一点:

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    Dialog dialog = new Dialog(getActivity());
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    dialog.setTitle(null);
    dialog.setContentView(R.layout._task);
    dialog.setIndeterminate(false);
    return dialog;
}

此外,您可以尝试使用Dialog而不是DialogFragment:

public void showProgressDialog() { 
   Dialog dialog = new Dialog(getActivity());
   dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
   dialog.setTitle(null);
   dialog.setContentView(R.layout._task);
   dialog.setIndeterminate(false);
   dialog.show();
}
相关问题