RecyclerView GridLayout适配器 - 如何使项目居中

时间:2014-12-23 14:17:04

标签: android android-support-library android-recyclerview

我正在使用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不起作用,它仍然使用所有可用空间。我想应该是LayoutManager负责在RecyclerView中设置子重力。

1 个答案:

答案 0 :(得分:2)

您已使用该行:

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中只有一个布局,并且您将项目的rootView声明为

android:width="match_parent"
android:height="match_parent"

所以这只是一个信息给你;)

如果您需要更好的支持,请提供更多信息。希望它无论如何都有帮助