My code show space on top of listview (see left image). If I remove android:gravity="center"
then it will show correctly (see right image)
But ProgressBar does not display on center of screen, it's displayed on the left of screen.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ListView
android:id="@+id/listCategory2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fadeScrollbars="true"
android:fastScrollEnabled="true"
android:divider="@color/background"
android:dividerHeight="1dp"
android:visibility="gone"
android:background="@drawable/border9"
android:listSelector="@drawable/listview_selector" />
<ProgressBar
android:id="@+id/prgLoading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@android:style/Widget.ProgressBar.Inverse"
android:layout_centerInParent="true" />
<TextView
android:id="@+id/txtAlert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/alert"
android:textColor="@color/text"
android:textSize="14sp"
android:layout_centerInParent="true"
android:visibility="gone" />
</LinearLayout>
The parent layout which you have taken is LinearLayout and the method in your xml android:layout_centerInParent="true" works for RelativeLayout. You need to change it to android:layout_gravity="center". You can instead change the parent layout to RelativeLayout.
You can use <FrameLayout>
, it really good for you.
And set progressbar (need match_parent
for layout_width
and layout_height
) as :
<ProgressBar
android:id="@+id/prgLoading"
android:layout_width="match_parent"
android:layout_height="match_parent"
style="@android:style/Widget.ProgressBar.Inverse"
android:layout_centerInParent="true"/>
Hope to help,
使其成为RelativeLayout
而不是LinearLayout
,并为ListView
添加android:layout_alignParentTop="true"
并为进度条添加android:layout_centerInParent="true"
<ProgressBar
android:id="@+id/loading_spinner"
style="@style/Widget.AppCompat.ProgressBar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
/>
For Linear layout
Simple way to center it inside a ConstraintLayout
<androidx.core.widget.ContentLoadingProgressBar
android:id="@+id/myProgress"
android:layout_width="100dp"
android:layout_height="100dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"/>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.