在RelativeLayout中的listview顶部显示进度条

时间:2013-08-30 01:49:59

标签: android android-layout listview progress-bar relativelayout

我在RelativeLayout中有一个listview,它从AsyncTask自动加载内容。现在我想在listview的顶部添加一个水平进度条,这样当元素加载时,它会显示进度(不在叠加层上)。我尝试了一些方法,但进度条没有正确显示。什么是正确的方法?

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

3 个答案:

答案 0 :(得分:2)

使用LinearLayout并在开始或完成加载时切换ProgressBar的可见性。

目前,您在RelativeLayout的上边缘都有两种观看结果。

答案 1 :(得分: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);
        }

答案 2 :(得分:0)

ProgressBarListView都与顶部对齐。在这种情况下,ListView将与ProgressBar重叠。尝试将ListView对齐到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>

对于您的情况,您最好使用LinearLayout代替RelativeLayout