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> 
相关问题