问题
I have a ProgressBar in a ScrollView, and I'd like it to stay centered vertically despite the user scrolling down or up. However, I'm not sure how to accomplish this.
Here's the code that I have, simple as it is. I have an indeterminate ProgressBar:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ocr_scroll_view"
android:layout_width="match_parent"
android:layout_height="fill_parent">
<RelativeLayout android:id="@+id/image_holder"
android:layout_gravity="end"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ProgressBar android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:visibility="gone"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
And I call it as such:
spinningProgressBar = (ProgressBar)findViewById(R.id.progressBar1);
spinningProgressBar.setIndeterminate(true);
spinningProgressBar.setVisibility(View.GONE);
Setting its visibility at the appropriate times.
All suggestions very much appreciated.
回答1:
Put it outside of the ScrollView. Just have the ScrollView and the ProgressBar overlap.
You can read about how to overlap views here: Overlapping Views in Android
There's a couple of ways to do it, ranging from GridLayouts to RelativeLayouts and then programatic ways to do it.
回答2:
You need the ScrollView to be at same level as ProgressDialog in your Layout.
Here it is in concept:
<RelativeLayout>
<ProgressDialog>
<ScrollView>
<RelativeLayout>
回答3:
you can insert this into the scrollview tag
android:fillViewport="true"
来源:https://stackoverflow.com/questions/27476534/how-to-make-a-progressbar-stay-in-the-center-of-screen-despite-scrolling