【问题标题】:RecyclerView GridLayoutAdapter - How to center itemsRecyclerView GridLayout Adapter - 如何使项目居中
【发布时间】:2014-12-23 14:17:04
【问题描述】:

我正在使用带有 GridLayoutAdapter 的 RecyclerView。是否可以将项目水平或垂直居中?

我的项目布局:

<?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 的责任。

【问题讨论】:

  • 曾经得到过有效的答案吗?
  • 没有。我最终硬编码 RecyclerView 宽度并将其水平居中在容器中。

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


【解决方案1】:

你已经对那行做了:

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"

所以这只是给你的信息;)

如果您需要更好的支持,请提供更多信息。希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-25
    • 1970-01-01
    相关资源
    最近更新 更多