android - 如何在RecyclerView项目中同时创建垂直和水平的layout_weight

标签 android android-layout android-recyclerview

我想要 RecyclerView 项目的布局如下图所示: UI Layout

下面是我的 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>

需要进行哪些更改才能使用线性布局同时在布局上应用垂直和水平权重?

最佳答案

如果你想使用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>

它看起来像这样:

enter image description here

关于android - 如何在RecyclerView项目中同时创建垂直和水平的layout_weight,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59527936/

相关文章:

android - 在确定的位置将 Textview 添加到 FrameLayout

java - 将值分配给列表<String[]>

android - 旋转木马android错误: No resource identifier found for attribute 'UseReflection' in package

安卓 : Drawable next to TextView with weight to each TextView in LinearLayout

java - RecyclerView 的项目显示不正确

java - 无法从 sqlite 表获取值

java - 在 Android 中将图像缩略图存储到图像中

Android——缩放屏幕布局的常用技巧

android - 在 CardView 中滚动 RecyclerView 非常慢

android - 优化显示在 RecyclerView 中的项目的复杂布局