我有一个包含 EditText
、Button
和 ListView
的 View 。
Button的onClick()
解析了一些站点(这部分工作正常),但是在ListView
中显示解析的信息需要一些时间,所以我想显示小的 ProgressBar
放在 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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/layout1"
android:weightSum="1.0">
<EditText
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight=".86" />
<ImageButton
android:layout_height="match_parent"
android:layout_width="0px"
android:layout_weight=".14" />
</LinearLayout>
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_below="@id/layout1"
android:gravity="center"
android:layout_centerInParent="true"
android:id="@+id/progressbar" >
<ProgressBar
android:layout_height="wrap_content"
android:layout_width="wrap_content"
style="@android:style/Widget.ProgressBar.Small" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/layout1">
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
</RelativeLayout>
在源代码中:
progressBar = (RelativeLayout) findViewById(R.id.progressbar);
每次我想显示 ProgressBar
时,我都会这样做:
progressBar.setVisibility(View.VISIBLE);
用于禁用:
progressBar.setVisibility(View.INVISIBLE);
但这行不通。
我该如何解决这个问题?
最佳答案
无需操纵您的 ProgressBar
的可见性。 ListView
具有称为“空 View ”的功能,仅当您的 ListView
为空时才会显示。以下是您需要执行的操作:
- 如果您的 Activity 扩展
ListActivity
,请使用android:id="@android:id/list"
用于您的ListView
和android:id="@android:id/empty"
用于您的ProgressBar
。 - 如果您不扩展
ListActivity
,则有一个setEmptyView()
ListView
的方法,它可以让您做同样的事情。
如果您不需要清空 ListView
(例如,数据仅在 Activity 启动时加载一次),则上述方法最适合。如果之后需要设置数据,最好使用 ViewSwitcher
,它也有很大的优势 - 它可以让你应用过渡动画。有关 ViewSwitcher
的详细信息,请查看 this .
关于android - 如何在做一些工作时在 View 上显示 ProgressBar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13709992/