将ProgressBar放在ConstraintLayout内的RecyclerView中

问题描述 投票:0回答:3

我有一个ProgressBarRecyclerView的一个项目,并在那里添加了适配器遵循本教程Android RecyclerView dynamically load more items when scroll to end with bottom ProgressBar

但是我没有在RelativeLayout文件中使用activity_main.xml,而是使用默认的ConstraintLayout。对我来说问题是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

java android android-recyclerview android-progressbar android-constraintlayout
3个回答
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
投票

您需要将重力添加到您的父LinearLayout,它将子的重力设置为居中。我已更正了进度条布局。希望这可以帮助。

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

0
投票

你想在屏幕中间居中吗?或者只是在线性布局内尝试重力而不是布局重力,这将尝试在线性布局内部而不是约束布局。因为你已经有匹配的父宽度。同时发布代码以显示何时将进度条添加到视图中。

© www.soinside.com 2019 - 2024. All rights reserved.