简体   繁体   中英

TextView inside LinearLayout not working properly

Just getting started with android and facing a little issue of how to set constraints.

I've got a label, image1(button) & image2(button). The image2 is left aligned to the screen. The label is first control and right-aligned and image1 is placed just beside the label. All I need is that if the label's text grows then image1 should stay beside it and at/before the screen end the label's text should go into the 2nd line so that image1 doesn't go outside the screen. But as soon as the text grows both of the images ie, image1 & image2 goes out of the screen.

<?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">

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/container_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginStart="@dimen/_8sdp"
    android:layout_marginEnd="@dimen/_8sdp"
    android:background="@color/light_blue"
    android:paddingTop="@dimen/_10sdp"
    android:paddingBottom="@dimen/_10sdp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="@dimen/_14sdp"
        android:layout_marginEnd="@dimen/_20sdp"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/task_name_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/rubik_regular"
            android:gravity="center"
            android:layout_marginTop="@dimen/_4sdp"
            android:textAlignment="textStart"
            android:text="Use the toilet paper"
            android:textColor="@color/new_app_black"
            android:textSize="@dimen/_14sdp"
            tools:ignore="RtlCompat"/>

        <ImageView
            android:id="@+id/audio_iv"
            android:layout_width="@dimen/_20sdp"
            android:layout_height="@dimen/_20sdp"
            android:minWidth="@dimen/_20sdp"
            android:layout_gravity="center"
            android:layout_marginStart="@dimen/_8sdp"
            android:layout_marginEnd="@dimen/_12sdp"
            android:src="@drawable/ic_audio" />

        <Space
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>

        <ImageView
            android:id="@+id/checked_iv"
            android:minWidth="@dimen/_24sdp"
            android:layout_width="@dimen/_24sdp"
            android:layout_height="@dimen/_24sdp"
            android:layout_gravity="center"
            android:src="@drawable/ic_task_unchecked" />

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

在此处输入图片说明

在此处输入图片说明

If you are using Constraint layout then you can remove LinearLayout and use all the children directly inside a constraint layout.

<?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"
    android:background="@color/light_grey">

    <TextView
        android:id="@+id/task_name_tv"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/_2sdp"
        android:layout_marginTop="@dimen/_4sdp"
        android:layout_marginEnd="@dimen/_2sdp"
        android:fontFamily="@font/poppins_regular"
        android:gravity="center"
        android:text="Use the toilet paper"
        android:textAlignment="textStart"
        android:textColor="@color/black"
        android:textSize="@dimen/_14sdp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/audio_iv"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="RtlCompat" />

    <ImageView
        android:id="@+id/audio_iv"
        android:layout_width="@dimen/_20sdp"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:layout_marginStart="@dimen/_8sdp"
        android:layout_marginEnd="@dimen/_12sdp"
        android:minWidth="@dimen/_20sdp"
        android:src="@drawable/ic_search"
        app:layout_constraintBottom_toBottomOf="@+id/task_name_tv"
        app:layout_constraintEnd_toStartOf="@+id/space"
        app:layout_constraintTop_toTopOf="@+id/task_name_tv" />

    <Space
        android:id="@+id/space"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1"
        app:layout_constraintBottom_toBottomOf="@+id/task_name_tv"
        app:layout_constraintEnd_toStartOf="@+id/checked_iv"
        app:layout_constraintTop_toTopOf="@+id/task_name_tv" />

    <ImageView
        android:id="@+id/checked_iv"
        android:layout_width="@dimen/_24sdp"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:minWidth="@dimen/_24sdp"
        android:src="@drawable/ic_chat_icon"
        app:layout_constraintBottom_toBottomOf="@+id/task_name_tv"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/task_name_tv" />

</androidx.constraintlayout.widget.ConstraintLayout>

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM