简体   繁体   中英

Horizontally and vertically center a ProgressBar in Relative Layout

I am trying to achieve the following progress dialog (without the text)

在此处输入图片说明

My current XML code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/background"
    android:orientation="vertical"
    >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/background"
        >

        <ProgressBar
            android:id="@+id/progress_bar"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_centerVertical="true"
            android:visibility="gone"
            />

I am programatically hiding and showing the progress bar when required (and hiding/showing other screen elements), all of which are under the Relative Layout in the XML code above.

However, whenever the Progress Bar shows, it appears centered at the top of the screen instead of being centered horizontally AND vertically.

Setting the ScrollView's attribute "fillViewport" to true should do it.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/background"
android:orientation="vertical"
android:fillViewport="true"
>

also make sure its child's height is set to "fill_parent" and the ProgressBar is properly centered

android:layout_centerVertical="true"
android:layout_centerHorizontal="true"

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM