在 ConstraintLayout 内的 RecyclerView 内居中 ProgressBar
Center ProgressBar inside RecyclerView that is inside ConstraintLayout
我有一个 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
的截图(需要居中):
您是否试图居中于屏幕中间?
或者就在线性布局里面
尝试重力而不是布局重力,这将尝试在线性布局而不是约束布局内居中。因为你已经在上面匹配了父宽度。还有 post 将进度条添加到视图时显示的代码。
我仔细查看了您的问题,目前还不清楚是怎么回事。我已经采用了示例项目并将您的更改应用于布局,并且效果很好。 (我确实删除了 tools
引用并更改了几个 ID。)
这里是 GitHub project with the changes just to the two layouts above. Here is a video of this modified app,它按预期在中心显示进度条。
我认为这是一个约束问题,但正如您从演示中看到的那样,情况并非如此。
这也可能与您 运行 的 ConstraintLayout
版本或其他一些配置有关,因此您可能需要检查一下。否则,看起来还有其他事情正在发生,但尚未透露。您是否对布局小部件进行任何其他操作?
了解布局中的哪些内容未对齐会很有用。给 LinearLayout
一个背景颜色,看看它是否在屏幕上延伸。这将告诉您进度条是否未对齐或 LinearLayout
是否被挤压。
另一种可能性:您是否没有在 LinearLayout
的 inflation 中命名 parent?像这样说:
View view = LayoutInflater.from(activity).inflate(R.layout.item_loading, null, false);
而不是这个
View view = LayoutInflater.from(activity).inflate(R.layout.item_loading, parent, false);
这个微小的差异会将您的进度条推到一边。
您需要为您的父级 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>
我有一个 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
的截图(需要居中):
您是否试图居中于屏幕中间? 或者就在线性布局里面 尝试重力而不是布局重力,这将尝试在线性布局而不是约束布局内居中。因为你已经在上面匹配了父宽度。还有 post 将进度条添加到视图时显示的代码。
我仔细查看了您的问题,目前还不清楚是怎么回事。我已经采用了示例项目并将您的更改应用于布局,并且效果很好。 (我确实删除了 tools
引用并更改了几个 ID。)
这里是 GitHub project with the changes just to the two layouts above. Here is a video of this modified app,它按预期在中心显示进度条。
我认为这是一个约束问题,但正如您从演示中看到的那样,情况并非如此。
这也可能与您 运行 的 ConstraintLayout
版本或其他一些配置有关,因此您可能需要检查一下。否则,看起来还有其他事情正在发生,但尚未透露。您是否对布局小部件进行任何其他操作?
了解布局中的哪些内容未对齐会很有用。给 LinearLayout
一个背景颜色,看看它是否在屏幕上延伸。这将告诉您进度条是否未对齐或 LinearLayout
是否被挤压。
另一种可能性:您是否没有在 LinearLayout
的 inflation 中命名 parent?像这样说:
View view = LayoutInflater.from(activity).inflate(R.layout.item_loading, null, false);
而不是这个
View view = LayoutInflater.from(activity).inflate(R.layout.item_loading, parent, false);
这个微小的差异会将您的进度条推到一边。
您需要为您的父级 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>