android - 均匀分布网格以适合屏幕

标签 android android-layout android-recyclerview

我试图在我的回收站 View 中获得三行以完美适合屏幕。换句话说,均匀分布,没有滚动。我有一个工具栏和一个带有两列/三行的回收 View 。截至目前,我只是将项目高度设置为足够接近屏幕,但必须有更好的方法吗?

activity_main.xml

<RelativeLayout
    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"
    tools:context=".MainActivity">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_below="@id/toolbar"
        android:background="#CCCC"
        android:layout_width= "match_parent"
        android:layout_height = "match_parent"/>

</RelativeLayout>

项目.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:id="@+id/colorBlock"
        android:layout_width="match_parent"
        android:layout_height="170dp"
        android:background="#CCCCCC" />

    <ImageView
        android:id="@+id/item"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_centerHorizontal="true"
        android:padding="16dp"
        />

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:textColor="@android:color/white"
        android:textSize="16sp"/>
</RelativeLayout>

最佳答案

根据定义,RecyclerView 应该用于可能发生回收 View 的情况 - 也称为可滚动,因为有大量 View 。我并不是说它不能用于较小的集合,但在其他布局上使用它没有任何值(value)。事实上,不是为这种场景构建的,在这个问题这样的场景中使用它变得非常困难。如果您绝对想使用 RecyclerView,我不知道有什么方法可以仅在 xml 中执行此操作。我认为您必须为此实现自己的布局管理器。

如果您愿意使用其他布局,这里有一个涉及大量线性布局和权重的解决方案。不是最优的,必须使用嵌套权重,但仍然是一个解决方案:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_below="@id/toolbar"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#ff0000"
        android:orientation="horizontal">
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#fff000"/>
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#ff0f00"/>
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#ff00f0"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#00ff00"
        android:orientation="horizontal">
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#f0ff00"/>
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#0fff00"/>
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#00fff0"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#0000ff"
        android:orientation="horizontal">
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#f000ff"/>
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#0f00ff"/>
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#00f0ff"/>
    </LinearLayout>
</LinearLayout>

关于android - 均匀分布网格以适合屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31990075/

相关文章:

java - 从文件中快速读取小端整数

java - 使用按钮在嵌套回收器 View 中动态添加子项

android - Firebase - RecyclerView 多次只显示一张图片

java - 我的 res/layout/main.xml 文件在哪里?

android - 带有包装内容项的 RecyclerView

java - 从固定选项卡中删除滑动操作

android - 具有自定义形状项目的 RecyclerView

android - 如何在我的 Activity 中存储来自 API 的数据

java - 在android中将Spinner String值 "20-34"转换为从20到34的整数

Android:包名用哪个? Manifest 的 packagename 或 Gradle applicationId?