RelativeLayout 占满宽度

RelativeLayout occupying full width

我正在使用以下布局,输出是这样的:

如何确保灰色背景仅适用于内容..为什么我为包含灰色背景的RelativeLayout指定wrap_content时它占据了整个宽度。

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="5dp"
    android:paddingRight="17dp"
    android:paddingLeft="17dp"

    android:paddingTop="5dp"
    android:weightSum="1">

    <RelativeLayout android:id="@+id/bubble" android:layout_gravity="right" android:gravity="right"

        android:layout_height="wrap_content" android:background="@drawable/back" android:layout_alignParentRight="true"

        android:layout_width="wrap_content">

        <ImageView
            android:id="@+id/user_reply_status"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/chat_user_reply"
            android:layout_marginBottom="5dp"
            android:layout_marginRight="10dp"
            android:layout_marginEnd="10dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"


            android:src="@drawable/ic_single_tick"
            android:visibility="visible" />

        <TextView
            android:id="@+id/user_reply_timing"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@id/chat_user_reply"

            android:paddingBottom="5dp"
            android:layout_marginRight="2dp"
            android:layout_marginEnd="2dp"
            android:layout_toLeftOf="@id/user_reply_status"
            android:layout_toStartOf="@id/user_reply_status"
            android:text="17:10" />

        <TextView
            android:id="@+id/chat_user_reply"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@id/user_reply_timing"

            android:autoLink="web"
            android:text="sample" />



    </RelativeLayout>
</RelativeLayout>

可能背景太大了?为什么不尝试将背景设置为仅文本字段

创建一个新的 ImageView 并将其放在底部的 RelativeLayout 中(第一项),并将其左边缘调整为您需要的 TextView。您也可以根据需要设置边距和填充。

您应该从第一个 RelativeLayout 中删除 android:weightSum="1"。

最好使用水平方向的 LinearLayout。试试这个布局代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:padding="5dp"
        android:background="@color/dark_gray">

        <TextView
            android:id="@+id/text1"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="sample sample sample sample sample sample sample sample sample sample"
            android:layout_marginRight="10dp"
            android:layout_gravity="center_vertical"
            android:textColor="@color/white"/>

        <TextView
            android:id="@+id/text2"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="17:10 17:10 17:10 17:10 17:10 17:10 17:10 17:10 17:10 17:10 17:10"
            android:layout_gravity="center_vertical"
            android:textColor="@color/white"/>

        <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/ic_lock_power_off"
            android:layout_gravity="center_vertical"/>
    </LinearLayout>
</RelativeLayout>