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

Android布局layout_below属性无法将文本放置在图片下方求助

问题根因
  • MaterialCardView 继承自 FrameLayout,仅支持帧布局规则,直接子控件会按添加顺序层叠摆放,本身不识别 layout_below 这类相对布局专属属性
  • 你代码里的 RelativeLayout 是直接闭合的空标签,ImageView、TextView 没有作为子控件放进 RelativeLayout 里,layout_below 属性只有在相对布局的直接子控件上才会生效
修复代码

你只需要把图片、文本控件都移入相对布局内部即可:

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:padding="20dp"
    android:id="@+id/parent"
    app:cardCornerRadius="7dp"
    app:cardElevation="7dp"
    android:layout_margin="5dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:layout_width="130dp"
            android:layout_height="150dp"
            android:id="@+id/imgFood"
            android:src="@mipmap/ic_launcher"
            android:contentDescription="@string/todo" />

        <TextView
            android:id="@+id/txtMealName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/imgFood"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="7dp"
            android:text="@string/meal_name"
            android:textSize="16sp"
            android:textStyle="bold" />

    </RelativeLayout>

</com.google.android.material.card.MaterialCardView>

内容的提问来源于stack exchange,提问作者Maxine

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 12:18:02