使 TextView 与父布局一样大

Make TextView as big as parent layout

我有一个 LinearLayout XML,如下所示,我希望布局中的最后一个 TextView 与线性布局的整个宽度相匹配。我添加了一个可绘制对象作为 TextView 的背景。这是 XML 文件(我省略了不必要的代码)-

    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:id="@+id/received_interaction_row_layout"
    android:layout_height="wrap_content"
    android:foreground="?selectableItemBackground"
    android:focusable="true"
    android:clickable="true"
    android:orientation="vertical"
    android:background="@drawable/rounded_corner_red"
    android:layout_marginStart="20dp"
    android:layout_marginEnd="20dp"
    android:layout_marginTop="8dp"
    android:elevation="3dp"
    android:layout_marginBottom="8dp">
    <TextView
        android:drawableStart="@drawable/ic_contact"
        android:drawingCacheQuality="high"
        android:drawablePadding="15dp" ..... />
    <TextView
        android:layout_marginTop="@dimen/row_padding_vertical"
        android:layout_marginBottom="@dimen/row_padding_vertical"
        android:drawableStart="@drawable/ic_event" ..... />
    <TextView
        android:layout_marginBottom="@dimen/row_padding_vertical"
        android:drawableStart="@drawable/ic_feedback"
        android:drawablePadding="15dp" ...... />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/rounded_bottom_pink"/>
    </LinearLayout>

这是目前的样子 -

我想删除最后一个 TextView(深色背景的)的填充,并使其看起来与父布局一样宽。我也想删除底部填充。

可绘制背景中有一些填充,但我也尝试将其删除,但没有用。

编辑 1 - 以下是我想要实现的目标。请注意底部的深蓝色背景。

Remove margin from parent LinearLayout. Add these margin to TextView.

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent"
android:id="@+id/received_interaction_row_layout"
android:layout_height="wrap_content"
android:foreground="?selectableItemBackground"
android:focusable="true"
android:clickable="true"
android:orientation="vertical"
android:background="@drawable/rounded_corner_red"
android:layout_marginTop="8dp"
android:elevation="3dp"
>
<TextView
    android:drawableStart="@drawable/ic_contact"
    android:drawingCacheQuality="high"
    android:layout_marginStart="20dp"
    android:layout_marginEnd="20dp"
    android:drawablePadding="15dp" ..... />
<TextView
    android:layout_marginTop="@dimen/row_padding_vertical"
    android:layout_marginBottom="@dimen/row_padding_vertical"
    android:layout_marginStart="20dp"
    android:layout_marginEnd="20dp"
    android:drawableStart="@drawable/ic_event" ..... />
<TextView
    android:layout_marginBottom="@dimen/row_padding_vertical"
    android:drawableStart="@drawable/ic_feedback"
    android:layout_marginStart="20dp"
    android:layout_marginEnd="20dp"
    android:drawablePadding="15dp" ...... />
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_marginBottom="8dp"
    android:background="@drawable/rounded_bottom_pink"/>
</LinearLayout>

大多数矢量可绘制对象可能包含自己的填充。 您可能需要更改 rounded_corner_red.xml。检查下面的矢量文件

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ff0090" />
<corners
    android:radius="5dp" />
<padding
    android:left="10dp"
    android:top="10dp"
    android:right="10dp"
    android:bottom="10dp" />
</shape>

您需要使用不带填充的矢量文件,请参考下面

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ff0090" />
<corners
    android:radius="5dp" />
</shape>

我终于修好了。事实证明,填充存在于导致此问题的父 LinearLayout 的背景中。 TextView 的可绘制背景不包含填充,但 LinearLayout 的背景包含填充,这导致了上述问题。我从作为父布局背景的 "rounded_corner_red" 可绘制对象中删除了填充。