TextView背景不包装内容

时间:2018-10-29 08:53:25

标签: android textview android-constraintlayout

我在ConstraintLayout里面有一个TextView,带有一个背景可绘制的。 它的宽度设置为0dp (match_constraint)。如果将其设置为wrap_content,则文本将在末尾剪辑。

我希望背景的大小只能为TextView

This is how I'm getting it

XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:layout_height="match_parent">

    <TextView
        android:id="@+id/message_receive"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:background="@drawable/rounded_chat_message"
        android:padding="8dp"
        android:textAppearance="@style/TextAppearance.AppCompat.Light.SearchResult.Subtitle"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        tools:text="This is a received message"/>

</androidx.constraintlayout.widget.ConstraintLayout>

将其设置为wrap_content后:

enter image description here

编辑:

rounded_chat_message.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">

            <gradient android:angle="0"
                android:startColor="@android:color/darker_gray"
                android:endColor="@android:color/darker_gray" />
            <corners android:radius="16dp" />
            <padding
                android:bottom="2dp"
                android:left="2dp"
                android:right="2dp"
                android:top="2dp" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@android:color/white" />
            <corners android:radius="16dp" />
        </shape>
    </item>
</layer-list>

4 个答案:

答案 0 :(得分:1)

在使用TextView来设置wrap_content属性时,要防止app:layout_constrainedWidth="true"被剪切。当宽度设置为wrap_content时,这将强制执行水平约束,因此必须同时设置startend(在您的情况下)。现在,要使TextView停留在开始位置,您需要设置app:layout_constraintHorizontal_bias="0"(或将1对准结尾)。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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"
    tools:layout_height="match_parent">

    <TextView
        android:id="@+id/message_receive"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:background="@drawable/rounded_chat_message"
        android:padding="8dp"
        android:textAppearance="@style/TextAppearance.AppCompat.Light.SearchResult.Subtitle"
        app:layout_constrainedWidth="true"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        tools:text="Message"/>

</androidx.constraintlayout.widget.ConstraintLayout>

答案 1 :(得分:0)

您使用的规则使视图延伸到父级末尾。 从TextView XML定义中删除此行,它应该看起来不错:

app:layout_constraintEnd_toEndOf="parent"

答案 2 :(得分:0)

放置此属性。

fetchPost

答案 3 :(得分:0)

将TextView放在其他布局中,并像这样对齐TextView以使其开始

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
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"
tools:layout_height="match_parent">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <TextView
        android:id="@+id/message_receive"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_margin="20dp"
        android:background="#ded"
        android:padding="20dp"
        android:textAppearance="@style/TextAppearance.AppCompat.
        Light.SearchResult.Subtitle"
        tools:text="This is a received message" />

</RelativeLayout>

</android.support.constraint.ConstraintLayout>