我正在将 RecyclerView 与 GridLayoutAdapter 一起使用。是否可以将项目水平或垂直居中?
我的物品布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="@+id/item_photo_image_view"
android:layout_width="@dimen/custom_photo_item_size"
android:layout_height="@dimen/custom_photo_item_size"
android:background="@drawable/custom_image_background"
tools:src="@drawable/gallery_list_cars"/>
</LinearLayout>
实际上是这样的:http://oi61.tinypic.com/34pjmtc.jpg
所需的布局应如下所示:http://oi62.tinypic.com/7e2t.jpg
将RecyclerView设置为wrap_content不起作用,它仍然使用所有可用空间。我想在 RecyclerView 中设置子重力应该是 LayoutManager 的责任。
你已经用那行做了:
android:gravity="center"
有了这条线,您已经将
ImageView
水平和垂直居中了。
如果您只想要其中之一,请使用例如:
android:gravity="center_horizontal"
我不知道你的问题到底是什么,我们甚至不知道
android:layout_width="@dimen/custom_photo_item_size"
您的
dimensions.xml
... 中声明了哪个值
如果您想将整个
Item
居中,您需要声明:
android:layout_gravity="Center"
但这没有任何意义,因为您在
GridView-Cell
中只有一种布局,并且您将 Item 的 rootView
声明为
android:width="match_parent"
android:height="match_parent"
所以这只是给您的信息;)
如果您需要更好的支持,请提供更多信息。无论如何希望它有帮助
<androidx.constraintlayout.widget.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="wrap_content"
android:layout_gravity="center_horizontal">
Adapter的xml父布局集 安卓:layout_width =“match_parent” 机器人:layout_gravity =“center_horizontal”