在 onCreateDialog DialogFragment 中,Progress Dialog 的 setContentView 不起作用。

5

我正在使用以下代码来显示进度条,以满足我的需求:

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个回答

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;
}

此外,您可以尝试使用对话框而不是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();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接