如何根据相对布局中元素的总高度使垂直线可展开,Android?

如何根据相对布局中元素的总高度使垂直线可展开,Android?,android,android-layout,Android,Android Layout,我有一个如下所示的相对布局,当我将RelativeLayout高度硬编码为25dp时,它没有问题,如下图所示 我想要的是,当TextView变为多行时,红色水平线可以自动扩展高度,而不是硬编码高度 这是我的密码 <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <View android:id="@+

我有一个如下所示的相对布局,当我将
RelativeLayout
高度硬编码为25dp时,它没有问题,如下图所示

我想要的是,当
TextView
变为多行时,红色水平线可以自动扩展高度,而不是硬编码高度

这是我的密码

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

    <View
        android:id="@+id/line"
        android:layout_width="3dp"
        android:layout_height="match_parent"
        android:background="@color/blue" />

    <TextView
        android:id="@+id/TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="4dp"
        android:layout_marginStart="4dp"
        android:layout_toEndOf="@+id/line"
        android:layout_toRightOf="@+id/line"
        android:text="TextView"
        android:textColor="@color/black"
        android:textSize="14sp" />
</RelativeLayout>

当我将
RelativeLayout
height更改为
wrap\u content
时,垂直线将不可见


所以我的问题是,如何制作一条垂直的
线
哪个高度可以根据
文本视图的高度进行扩展?

您可以使用水平的线性布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <View
        android:id="@+id/line"
        android:layout_width="13dp"
        android:layout_height="match_parent"
        android:background="@color/md_blue_900" />

    <TextView
        android:id="@+id/TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="4dp"
        android:layout_marginStart="4dp"
        android:text="TextView"
        android:textColor="@color/md_black_1000"
        android:textSize="14sp" />
</LinearLayout>

您可以使用水平线性布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <View
        android:id="@+id/line"
        android:layout_width="13dp"
        android:layout_height="match_parent"
        android:background="@color/md_blue_900" />

    <TextView
        android:id="@+id/TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="4dp"
        android:layout_marginStart="4dp"
        android:text="TextView"
        android:textColor="@color/md_black_1000"
        android:textSize="14sp" />
</LinearLayout>

下面的代码对我很有用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
    <View
        android:id="@+id/line"
        android:layout_width="3dp"
        android:layout_height="match_parent"
        android:background="#cd2121" />

    <TextView
        android:id="@+id/TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="4dp"
        android:layout_marginStart="4dp"
        android:text="TextView \n tegdienaifa \n nfuyeuayeyae \n tefgaeyigufYK"
        android:textSize="14sp" />
</LinearLayout>

下面的代码对我很有用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
    <View
        android:id="@+id/line"
        android:layout_width="3dp"
        android:layout_height="match_parent"
        android:background="#cd2121" />

    <TextView
        android:id="@+id/TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="4dp"
        android:layout_marginStart="4dp"
        android:text="TextView \n tegdienaifa \n nfuyeuayeyae \n tefgaeyigufYK"
        android:textSize="14sp" />
</LinearLayout>

检查此项检查此项