【问题标题】:Center ProgressBar inside RecyclerView that is inside ConstraintLayout在 ConstraintLayout 内的 RecyclerView 中居中 ProgressBar
【发布时间】:2017-06-19 00:22:13
【问题描述】:

我有一个ProgressBar,它是RecyclerView 的一个项目,并在本教程Android RecyclerView dynamically load more items when scroll to end with bottom ProgressBar 之后与适配器一起添加到那里。

但我没有在activity_main.xml 文件中使用RelativeLayout,而是使用默认的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的截图(我需要它居中):

【问题讨论】:

  • 我再次查看了您的问题,认为我的回答并没有真正解决它。当您说进度条未居中时,您的屏幕是什么样的?能发个截图吗?

标签: java android android-recyclerview android-progressbar android-constraintlayout


【解决方案1】:

我仔细研究了您的问题,但完全不清楚发生了什么。我已经获取了示例项目并将您的更改应用于布局,它工作得很好。 (我确实删除了 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);

这个微小的差异会将你的进度条推到一边。

【讨论】:

  • inflater 中缺少的parent 是导致ProgressBar 未居中的主要原因。谢谢。
【解决方案2】:

您需要将重力添加到您的父 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>

【讨论】:

    【解决方案3】:

    你想在屏幕中间居中吗? 或者就在线性布局内 尝试重力而不是布局重力,它将尝试在线性布局而不是约束布局内居中。既然你已经有匹配的父宽度就可以了。还要发布代码以在您将进度条添加到视图时显示。

    【讨论】:

    • 将android:gravity 添加到ProgressBar 并没有解决它。并且ProgressBar 以编程方式添加到扩展RecyclerView.Adapter&lt;RecyclerView.ViewHolder&gt; 的适配器中(请参阅我在问题中发布的链接。除了activity_main.xml 中的ConstraintLayout 之外,它完全相同)。
    猜你喜欢
    • 2017-07-18
    • 1970-01-01
    • 2023-04-05
    • 2019-04-26
    • 1970-01-01
    • 2021-07-28
    • 1970-01-01
    • 1970-01-01
    • 2019-01-07
    相关资源
    最近更新 更多