我使用带有背景颜色的View在相对布局中创建垂直线。换行内容不起作用,该行将整个视口作为高度。为什么会这样?附上我的无效代码
<LinearLayout 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="wrap_content"
android:background="#f5f5f5"
android:orientation="vertical"
tools:ignore="MissingPrefix">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:layout_width="2dp"
android:layout_alignParentTop="true"
android:layout_height="wrap_content"
android:layout_marginLeft="21.5dp"
android:background="#a9382b" />
<TextView
android:id="@+id/seeall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"
android:textColor="#a9382b"
android:textSize="11sp"
tools:text="SEE ALL" />
</RelativeLayout>
</LinearLayout>
答案 0 :(得分:4)
在这种情况下,将高度设置为wrap_content
根本不起作用。
一般情况下,wrap_content
会为视图提供足够的大小以包含“内部”的所有内容(无论是子视图还是TextView
中的文本等),但是View
} tag没有任何“内部”来定义高度。在这种情况下,它将增长以填充所有可用空间。
如果您希望RelativeLayout
与其中的TextView
一样高,并且您希望该行从 大小的顶部到底部,请使用以下是您的View
代码:
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:layout_alignTop="@+id/seeall"
android:layout_alignBottom="@+id/seeall"
android:layout_marginLeft="21.5dp"
android:background="#a9382b">