相对布局中的ProgressBar水平和垂直居中

时间:2015-03-12 02:46:06

标签: java android xml

我正在尝试实现以下进度对话框(没有文本)

enter image description here

我目前的XML代码如下:

<?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"
            />

我以编程方式隐藏并在需要时显示进度条(并隐藏/显示其他屏幕元素),所有这些都在上面的XML代码中的相对布局下。

但是,只要进度条显示,它就会显示在屏幕顶部的中心位置,而不是水平和垂直居中。

1 个答案:

答案 0 :(得分:1)

设置ScrollView的属性&#34; fillViewport&#34;真的应该这样做。

<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"
>

还要确保其孩子的身高设置为&#34; fill_parent&#34;并且ProgressBar正确居中

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