【问题标题】:ProgressBar is not center in RelativeLayout when a ListView inside layout当 ListView 内部布局时,ProgressBar 不在 RelativeLayout 中
【发布时间】:2019-10-10 20:11:33
【问题描述】:

我有以下 xml:

<?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"
  android:orientation="vertical">

  <include
    android:id="@+id/toolbar"
    layout="@layout/toolbar_layout" />

  <ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/toolbar"
    android:gravity="center_horizontal"
    android:padding="@dimen/dimen_16dp"
    android:textSize="@dimen/dimen_16sp" />

  <ProgressBar
    android:id="@+id/progress_bar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:visibility="visible" />
</RelativeLayout>

我在它下面有一个工具栏和一个 listView。在我填充列表之前,我想在布局的中心显示一个 ProgressBar。我设置了 centerInParent 但它不起作用。进度条显示在工具栏上方。

我尝试将 progressBar 放在 LinearLayout 中,但得到了相同的结果。怎样才能居中?

编辑

这是toolbar_layout.xml

  <?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/toolbar"
  android:layout_width="match_parent"
  android:layout_height="@dimen/toolbar_height"
  android:background="@color/colorPrimary"
  app:contentInsetStart="0dp">

  <ImageButton
    android:id="@+id/home"
    android:layout_width="@dimen/margin_16dp"
    android:layout_height="match_parent"
    android:background="@android:color/transparent"
    android:src="@drawable/ic_arrow_back_black_16dp"
    android:layout_marginStart="@dimen/margin_16dp"
    tools:ignore="contentDescription" />

  <TextView android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="@dimen/margin_16dp"
    style="@style/ScreenTitleStyle"
    android:textColor="@color/textColorPrimary"/>
</androidx.appcompat.widget.Toolbar>

【问题讨论】:

  • 在此处发布您的工具栏布局代码。
  • @MehulSolanki 我编辑了我的问题并添加了相关的工具栏布局。
  • 我在下面添加了答案。检查剪断。如果这可行,请取消注释您的工具栏,并删除我使用的工具栏。

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


【解决方案1】:

试试下面的截图

<?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"
   android:gravity="center"
   android:orientation="vertical">

   <android.support.v7.widget.Toolbar
        android:layout_alignParentTop="true"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:elevation="4dp"
        android:id="@+id/toolbar" />
   <!-- <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar_layout" />-->
   <ListView
       android:id="@+id/list"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_below="@id/toolbar"
       android:gravity="center_horizontal"
       android:padding="16dp"
       android:textSize="16sp" />

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

</RelativeLayout>

您的列表视图hightwidth 必须是match_parent

【讨论】:

  • 这就是问题所在,高度是 wrap_content 和 match_parent 解决了居中问题。谢谢!!
【解决方案2】:
<?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"
  android:orientation="vertical">

  <include
    android:id="@+id/toolbar"
    layout="@layout/toolbar_layout"
    android:layout_width="match_parent"
     android:layout_height="?android:actionBarSize" />

  <ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/toolbar"
    android:gravity="center_horizontal"
    android:padding="@dimen/dimen_16dp"
    android:textSize="@dimen/dimen_16sp" />

  <ProgressBar
    android:id="@+id/progress_bar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:visibility="visible" />
</RelativeLayout>

【讨论】:

    【解决方案3】:

    如果您不介意使用约束布局,那么以下应该可以解决问题:

    <?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">
        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar_layout" />
    
        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/toolbar"
            android:gravity="center_horizontal"
            android:padding="@dimen/dimen_16dp"
            android:textSize="@dimen/dimen_16sp"
            app:layout_constraintTop_toBottomOf="@id/toolbar"
            app:layout_constraintBottom_toBottomOf="parent"/>
    
        <ProgressBar
            android:id="@+id/progress_bar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:visibility="visible"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    </android.support.constraint.ConstraintLayout>
    

    【讨论】:

      【解决方案4】:

      从您的 ProgressBar 中删除 android:layout_below="@id/toolbar"

       <?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">
      
          <include
              android:id="@+id/toolbar"
              android:layout_width="match_parent"
              android:layout_height="?actionBarSize"
              layout="@layout/bb" />
      
          <ListView
              android:id="@+id/list"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_below="@id/toolbar"
              android:padding="16dp"
              android:textSize="16sp" />
      
          <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:layout_below="@id/toolbar" 它显示在工具栏下方,但不是我想要的屏幕中心。没有它,progressBar 会显示在工具栏上。
      猜你喜欢
      • 1970-01-01
      • 2010-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多