【问题标题】:How to center a progressbar in screen如何在屏幕中居中进度条
【发布时间】:2020-01-13 12:08:51
【问题描述】:

进度条出现在屏幕左侧。我希望它居中,就像在示例中一样。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:gravity="center"
        android:background="@drawable/semi_circle"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

<RelativeLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_marginTop="50dp"
        android:layout_gravity="center_horizontal"
        android:id="@+id/relative">

        <ProgressBar
                android:id="@+id/progress_bar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:visibility="visible" />


    <LinearLayout android:layout_width="match_parent"
                  android:id="@+id/lilPolicy"
                  android:layout_height="match_parent"
                  android:layout_marginBottom="20dp">


    </LinearLayout>

【问题讨论】:

  • 你能分享完整的布局代码
  • 添加 android:layout_centerInParent="true" 属性使其居中。

标签: android android-layout android-relativelayout android-scrollview android-progressbar


【解决方案1】:

在你的滚动视图中

使用 RelativeLayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

</RelativeLayout>

使用 ConstraintLayout

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

【讨论】:

    【解决方案2】:

    像使用 RelativeLayout 一样使用 android:layout_centerInParent="true"

    【讨论】:

    • 或者你可以使用 android:layout_centerHorizo​​ntal="true"
    【解决方案3】:

    如果您使用相对布局作为父布局。所以我们需要添加这个属性android:layout_centerInParent="true" 来对齐父中心的视图。默认情况下,滚动视图会删除视图中的额外间距。添加android:fillViewport="true" 使滚动视图匹配父级

    试试这个代码

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:gravity="center"
        android:background="#000"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">
    
        <RelativeLayout
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_marginTop="50dp"
            android:layout_gravity="center_horizontal"
            android:id="@+id/relative">
    
            <ProgressBar
                android:id="@+id/progress_bar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
              android:layout_centerInParent="true"
                android:visibility="visible" />
    
    
            <LinearLayout android:layout_width="match_parent"
                android:id="@+id/lilPolicy"
                android:layout_height="match_parent"
                android:layout_marginBottom="20dp"
                android:orientation="horizontal">
    
    
            </LinearLayout>
        </RelativeLayout>
    </ScrollView>
    

    【讨论】:

      【解决方案4】:

      将 ConstraintLayout 添加到您的项目中,Build a Responsive UI with ConstraintLayout

      您可以轻松实现它。

      我会这样做的方式是:

      <?xml version="1.0" encoding="utf-8"?>
      <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
      
          <ProgressBar
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintLeft_toLeftOf="parent"
              app:layout_constraintRight_toRightOf="parent"
              app:layout_constraintTop_toTopOf="parent" />
      
          <ScrollView
              android:layout_width="0dp"
              android:layout_height="0dp"
              android:fillViewport="true"
              android:orientation="vertical"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintLeft_toLeftOf="parent"
              app:layout_constraintRight_toRightOf="parent"
              app:layout_constraintTop_toTopOf="parent">
      
              <android.support.constraint.ConstraintLayout
                  android:layout_width="match_parent"
                  android:layout_height="match_parent">
      
                  <LinearLayout
                      android:id="@+id/item_1"
                      android:layout_width="match_parent"
                      android:layout_height="80dp"
                      android:layout_marginRight="10dp"
                      android:layout_marginLeft="10dp"
                      android:layout_above="@id/item_2"
                      android:background="#0000FF"
                      android:layout_marginTop="10dp"
                      android:orientation="horizontal"
                      app:layout_constraintLeft_toLeftOf="parent"
                      app:layout_constraintTop_toTopOf="parent" />
      
                  <LinearLayout
                      android:id="@+id/item_2"
                      android:layout_width="match_parent"
                      android:layout_height="80dp"
                      android:layout_marginRight="10dp"
                      android:layout_marginLeft="10dp"
                      android:layout_marginTop="10dp"
                      android:layout_below="@id/item_1"
                      android:background="#FF0000"
                      android:orientation="horizontal"
                      app:layout_constraintTop_toBottomOf="@id/item_1" />
      
                  <LinearLayout
                      android:id="@+id/item_3"
                      android:layout_marginTop="10dp"
                      android:layout_width="match_parent"
                      android:layout_height="80dp"
                      android:layout_marginRight="10dp"
                      android:layout_marginLeft="10dp"
                      android:layout_below="@id/item_2"
                      android:background="#FFFF00"
                      android:orientation="horizontal"
                      app:layout_constraintTop_toBottomOf="@id/item_2" />
      
              </android.support.constraint.ConstraintLayout>
              >
          </ScrollView>
      </android.support.constraint.ConstraintLayout>
      

      【讨论】:

      • 添加说明
      • 避免嵌套!
      【解决方案5】:

      您应该在ScrollView 之外获取ProgressBar,因为从逻辑上讲,您可以在运行时知道ScrollView 的高度,因此在运行您的应用程序之前您无法确定ProgressBar 的位置。

      所以,从ScrollView 中取出ProgressBar,创建另一个根布局,将ProgressBarScrollView 都包裹在其中;我选择了RelativeLayout 作为根,如果你愿意,你可以决定另一个布局管理器。

      layout_centerInParent 属性用于在RelativeLayout 中使视图居中

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="@drawable/semi_circle"
          android:gravity="center">
      
          <ScrollView
              android:layout_width="match_parent"
              android:layout_height="match_parent">
      
              <RelativeLayout
                  android:id="@+id/relative"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_gravity="center_horizontal"
                  android:layout_marginTop="50dp">
      
                  <LinearLayout
                      android:id="@+id/lilPolicy"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      android:layout_marginBottom="20dp">
      
      
                  </LinearLayout>
      
              </RelativeLayout>
      
          </ScrollView>
      
      
          <ProgressBar
              android:id="@+id/progress_bar"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_centerInParent="true"
              android:visibility="visible" />
      
      </RelativeLayout>
      

      注意:我不知道你要完成什么,所以我在根布局中留下了android:background="@drawable/semi_circle

      【讨论】:

        猜你喜欢
        • 2014-03-24
        • 1970-01-01
        • 2021-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-04
        • 2015-07-11
        相关资源
        最近更新 更多