Android 如何在ListView中放置进度条?
我有一个用AsyncTask填充的ListView。在运行时,ListView变得不可见,ProgressBar显示在水平线的中心,在AsyncTask完成后消失,ListView再次显示。现在看起来是这样的: 但我需要将ProgressBar显示在ListView内部,并在水平和垂直方向居中: 以下是我的布局标记:Android 如何在ListView中放置进度条?,android,android-listview,markup,android-progressbar,Android,Android Listview,Markup,Android Progressbar,我有一个用AsyncTask填充的ListView。在运行时,ListView变得不可见,ProgressBar显示在水平线的中心,在AsyncTask完成后消失,ListView再次显示。现在看起来是这样的: 但我需要将ProgressBar显示在ListView内部,并在水平和垂直方向居中: 以下是我的布局标记: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<LinearLayout
android:id="@+id/LinearHeaderProgress"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone">
<ProgressBar
android:id="@+id/HeaderProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ProgressBar>
</LinearLayout>
<ListView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/listView"/>
</LinearLayout>
我怎样才能做到这一点
谢谢。试着像这样使用RelativeLayout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<ProgressBar
android:id="@+id/HeaderProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" >
</ProgressBar>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
另外,在onCreate()方法中,将进度条设置为空视图<代码>listView.setEmptyView(findViewById(R.id.HeaderProgress))代码>然后,当您的列表中充满数据时,进度条将消失。@hfann这是我今天学到的新东西,谢谢,我会试试。@Chitrang,谢谢,它有效@hfann,而不是.setEmptyView()
我使用.setVisibility(View.GONE)在onPostExecute()
方法中的code>。@hfann此行:listView.setEmptyView(findViewById(R.id.HeaderProgress));填充条目后将隐藏进度条,但如果listView为空,我们如何显示空文本(未找到记录)。您可以在布局中有一个TextView
,如果listView为空,则将其设置为空视图。确保隐藏进度条。