0

我在 RelativeLayout 中有一个列表视图,它自动从 AsyncTask 加载内容。现在我想在列表视图的顶部添加一个水平进度条,以便在加载元素时显示进度(而不是在覆盖上)。我尝试了一些方法,但进度条没有正确显示。正确的做法是什么?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
    <ProgressBar
    android:id="@+id/progressBar1"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/listViewTitle"
    android:layout_alignParentTop="true" />
<ListView
    android:id="@+id/listViewTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >
</ListView>

4

3 回答 3

2

在开始或结束加载时使用LinearLayout和切换可见性。ProgressBar

目前,您在RelativeLayout.

于 2013-08-30T02:28:22.183 回答
0

theProgressBar和 theListView都与顶部对齐。在这种情况下,ListView将与ProgressBar. 尝试将 对齐ListView到 as 下方ProgressBar

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingBottom="@dimen/activity_vertical_margin"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                tools:context=".MainActivity" >

    <ProgressBar android:id="@+id/progressBar1"
                 style="?android:attr/progressBarStyleHorizontal"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_alignLeft="@+id/listViewTitle"
                 android:layout_alignParentTop="true" />

    <ListView android:id="@+id/listViewTitle"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentLeft="true"
              android:layout_alignParentTop="true"
              android:layout_below="@id/progressBar1" />

</RelativeLayout>

对于您的情况,您最好使用 aLinearLayout而不是 a RelativeLayout

于 2013-08-30T02:26:37.870 回答
0

在代码中你可以这样做:

public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private ProgressDialog mProgressDialog;
----
----

   @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DIALOG_DOWNLOAD_PROGRESS:
            mProgressDialog = new ProgressDialog(this);
            mProgressDialog.setMessage("Downloading data file..");
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            mProgressDialog.setCancelable(false);
            mProgressDialog.show();
            return mProgressDialog;
        default:
            return null;
        }
    }

----
----
In AsyncTask:


    @Override
        protected void onPreExecute() {
            super.onPreExecute();
            showDialog(DIALOG_DOWNLOAD_PROGRESS);
        }

        @Override
        protected String doInBackground(String... urls) {

          ----
          ----
          publishProgress("" + progress);
        }

        protected void onProgressUpdate(String... progress) {
         mProgressDialog.setProgress(Integer.parseInt(progress[0]));
    }

        @Override
        protected void onPostExecute(String result) {   
            dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
        }
于 2013-08-30T01:56:39.790 回答