进度对话框 setContentView 在 onCreateDialog DialogFragment 中不起作用

AJT*_*EST 5 android progressdialog android-progressbar android-dialogfragment

我正在使用此代码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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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

Mar*_*ada 2

您可以使用基本对话框,例如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;
}
Run Code Online (Sandbox Code Playgroud)

另外,您可以尝试使用 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();
}
Run Code Online (Sandbox Code Playgroud)