首页 > 解决方案 > 如何在 RecyclerView 项目中同时为垂直和水平创建 layout_weight

问题描述

我想要 RecyclerView 的项目如下图所示的布局: 界面布局

下面是我的 XML 项目脚本:

  <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:app="http://schemas.android.com/apk/res-auto"
       android:orientation="horizontal" android:layout_width="match_parent"
       android:weightSum="1"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/t"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_weight="0.5"
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="20sp"
         />

    <TextView
        android:id="@+id/d"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_weight="0.5"
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="20sp"
         />

</LinearLayout>

需要进行哪些更改才能将垂直和水平权重同时应用于布局Linear Layout

标签: androidandroid-layoutandroid-recyclerview

解决方案


如果你想使用LinearLayout你可以使用这样的东西:

[LinearLayout ]   [LinearLayout ]
[child 1]         [child 1](takes full screen height)
[child 2]

这是它的 XML:

<LinearLayout
    android:orientation="vertical"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="2">

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="Button"/>

    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="Button" />

</LinearLayout>



<LinearLayout
    android:orientation="vertical"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1">

    <Button
        android:id="@+id/button4"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Button" />
    </LinearLayout>
</LinearLayout>

它看起来像这样:

在此处输入图像描述


推荐阅读