You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android列表行布局实现:左右固定图片+中间自适应内容

Android自定义行布局问题解决:左右固定ImageView+中间填充布局

需求说明

  • 左侧ImageView左边缘与行布局左边缘对齐,采用固定尺寸
  • 右侧ImageView右边缘与行布局右边缘对齐,采用固定尺寸
  • 中间区域填充左右ImageView之间的剩余空间,不能遮挡两侧ImageView

当前问题

使用嵌套LinearLayout实现时,中间布局设置match_parent后会占据全部剩余空间,导致右侧ImageView被遮挡。

当前代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/img_A"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:minWidth="70dp"
                android:minHeight="70dp"
                app:srcCompat="@drawable/ic_baseline_A_24" />

        </LinearLayout>

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

            <TextView
                android:id="@+id/lbl_textA"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView"
                android:textSize="20sp" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/lbl_textB"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="TextView"
                    android:textSize="14sp" />

                <TextView
                    android:id="@+id/lbl_num"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="4"
                    android:text="TextView"
                    android:textAlignment="textEnd" />

            </LinearLayout>

            <TextView
                android:id="@+id/lbl_date"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="TextView" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/img_B"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="20dp"
                android:layout_marginRight="15dp"
                android:adjustViewBounds="false"
                android:cropToPadding="false"
                android:minWidth="30dp"
                android:minHeight="30dp"
                app:srcCompat="@drawable/ic_baseline_B_24" />
        </LinearLayout>
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

解决方案

方案1:修正LinearLayout布局设置

核心是利用LinearLayout的layout_weight属性正确分配空间,避免中间布局挤压右侧:

  1. 将中间LinearLayout的layout_width从match_parent改为0dp,设置layout_weight后,0dp会让系统根据权重自动分配剩余空间,不会强制占据全部宽度。
  2. 移除右侧LinearLayout的layout_weight="0",固定宽度即可,无需权重设置。

修正后的代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <!-- 左侧固定ImageView容器 -->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/img_A"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:minWidth="70dp"
                android:minHeight="70dp"
                app:srcCompat="@drawable/ic_baseline_A_24" />

        </LinearLayout>

        <!-- 中间填充区域,用0dp+weight分配剩余空间 -->
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:orientation="vertical">

            <TextView
                android:id="@+id/lbl_textA"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView"
                android:textSize="20sp" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/lbl_textB"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="TextView"
                    android:textSize="14sp" />

                <TextView
                    android:id="@+id/lbl_num"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="TextView"
                    android:textAlignment="textEnd" />

            </LinearLayout>

            <TextView
                android:id="@+id/lbl_date"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView" />

        </LinearLayout>

        <!-- 右侧固定ImageView容器,固定宽度无需权重 -->
        <LinearLayout
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/img_B"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="20dp"
                android:layout_marginRight="15dp"
                android:adjustViewBounds="false"
                android:cropToPadding="false"
                android:minWidth="30dp"
                android:minHeight="30dp"
                app:srcCompat="@drawable/ic_baseline_B_24" />
        </LinearLayout>
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

方案2:改用ConstraintLayout实现(更推荐)

直接利用ConstraintLayout的约束关系实现,减少布局层级,更灵活:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- 左侧ImageView -->
    <ImageView
        android:id="@+id/img_A"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_baseline_A_24" />

    <!-- 中间填充区域 -->
    <LinearLayout
        android:id="@+id/ll_middle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="5dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/img_B"
        app:layout_constraintStart_toEndOf="@+id/img_A"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/lbl_textA"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textSize="20sp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/lbl_textB"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView"
                android:textSize="14sp" />

            <TextView
                android:id="@+id/lbl_num"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="TextView"
                android:textAlignment="textEnd" />

        </LinearLayout>

        <TextView
            android:id="@+id/lbl_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

    </LinearLayout>

    <!-- 右侧ImageView -->
    <ImageView
        android:id="@+id/img_B"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="20dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_baseline_B_24" />

</androidx.constraintlayout.widget.ConstraintLayout>

约束说明:

  • img_A:左对齐父布局,上下居中,固定宽高70dp
  • ll_middle:左对齐img_A的右边缘,右对齐img_B的左边缘,宽度设为0dp自动填充剩余空间
  • img_B:右对齐父布局,顶部对齐父布局(可根据需求调整垂直位置),固定宽高30dp

内容的提问来源于stack exchange,提问作者Learning from masters

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 03:35:32