java - 在相对布局中水平和垂直居中进度条

标签 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 代码中的相对布局下。

但是,每当进度条显示时,它都会显示在屏幕顶部居中,而不是水平和垂直居中。

最佳答案

将 ScrollView 的属性“fillViewport”设置为 true 就可以了。

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

还要确保其子项的高度设置为“fill_parent”并且 ProgressBar 正确居中

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

关于java - 在相对布局中水平和垂直居中进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29001044/

相关文章:

java - Eclipse RAP 应用程序中的浏览器检测

android - 如何制作进度圈

java - 如何获取 java 文件中的变量并将其连接到 XML 文件

java - 如何在 Scene Builder 中对齐所有文本字段?

java - 构建期间生命周期的 Maven 打印阶段

java - 当我使用字符串作为文件名时,eclipse 扫描仪 filenotfoundException

java - 安卓。保存自定义对象的小型 arrayList 的简单方法

android - Opencv提高阈值的准确性

android - 将 TURN 服务器添加到 android webRtc native

python - 查找 XML 标记值并选择包含所请求标记值的父级