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
相关产品推荐
相关产品推荐

