0

我有一個ProgressBar,它是RecyclerView的一個項目,並且在那裏添加了這個教程Android RecyclerView dynamically load more items when scroll to end with bottom ProgressBar後面的適配器。RecyclerView裏面的Center ProgressBar在ConstraintLayout裏面

但是,使用默認ConstraintLayout而不是在activity_main.xml文件中使用RelativeLayout。對我來說問題是ProgressBar不居中。

請找我上面所描述的佈局如下:

進度欄項目

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

    <ProgressBar 
     android:id="@+id/progressbar" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" /> 

</LinearLayout> 

的activity_main.xml中

<?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" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="me.bookquotes.quotes.MainActivity"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     android:background="?attr/colorPrimary" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" /> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/list" 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/toolbar" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" /> 

</android.support.constraint.ConstraintLayout> 

下面是截圖出現在左邊的ProgressBar(我需要它居中):

Non-centered progress bar

+0

我再看看你的問題,不認爲我的答案真的解決了它。當你說進度條不居中時,你的屏幕是什麼樣的?你能發佈一個屏幕截圖嗎? – Cheticamp

回答

2

我仔細看了一下你的問題,目前還不清楚究竟發生了什麼。我已經拿到了示例項目並將更改應用到了佈局,並且它工作得很好。 (我刪除了tools參考並更改了幾個ID。)

以下是GitHub project,其中僅更改了上述兩個佈局。這是一個video of this modified app,如預期的那樣在中心顯示進度條。

我在想這是一個約束問題,但是,從演示中可以看出,情況並非如此。

這也可能與您正在運行的ConstraintLayout版本或某些其他配置有關,因此您可能需要檢查。否則,看起來還有其他事情正在發生,但尚未透露。你是否在對佈局小部件進行其他操作?

知道你的佈局沒有排隊是有用的。給LinearLayout一個背景顏色,看它是否在屏幕上延伸。這會告訴你,如果進度條未對齊或者是否捏合了LinearLayout


另外一個可能性:你沒有來命名LinearLayout的通貨膨脹的家長嗎?說是這樣的:

View view = LayoutInflater.from(activity).inflate(R.layout.item_loading, null, false); 

,而不是這個

View view = LayoutInflater.from(activity).inflate(R.layout.item_loading, parent, false); 

那小的差異會推你的進度條到一邊。

+1

'inflater'中缺少'parent'是'ProgressBar'未居中的主要原因。謝謝。 – h4k1m

0

你們是不是在屏幕中間的中心? 或者只是在線性佈局 嘗試使用重力而不是佈局重力,它會嘗試將線性佈局而不是約束佈局置於中心。由於你已經有匹配父寬度。同時發佈代碼以顯示何時將進度欄添加到視圖。

+0

將'android:gravity'添加到'ProgressBar'沒有解決它。並且'ProgressBar'以編程方式添加到擴展'RecyclerView.Adapter '的適配器中(請參閱我在問題中發佈的鏈接,除了'activity_main.xml中的'ConstraintLayout'外,完全相同')。 – h4k1m

1

您需要向您的父LinearLayout添加重力,該重力將子重心設置爲center.I已更正進度欄佈局。 希望這可以幫助。

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:orientation="vertical"> 

     <ProgressBar 
      android:id="@+id/progressbar" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" /> 

    </LinearLayout> 
相關問題