简体   繁体   中英

Android - prevent TextView to push out another element inside LinearLayout

This is the Layout:

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/profileUserImage">

        <TextView
            android:id="@+id/profileNickname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:scrollHorizontally="false"
            android:text="No Nickname yet fdg d trdt rdrt tdr  td trd t"
            android:textColor="@color/colorText"
            android:textSize="24sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:id="@+id/btnEditNickname"
            android:layout_width="42dp"
            android:layout_height="42dp"
            android:layout_marginLeft="8dp"
            android:padding="6dp"
            android:scaleType="fitXY"
            app:srcCompat="@android:drawable/ic_menu_edit" />

    </androidx.appcompat.widget.LinearLayoutCompat>

When profileNickname is getting too long, it pushes out btnEditNickname out of the LinearLayout and also overlays itself out of the Layout a bit before the text is getting wrapped. How can I get this annoying piece of c*** to wrap IN TIME, before ruining everything?? I'm getting sick of wasting hours of time for minor bs like that, I hate android!

You just need to add this line to the TextView android:layout_weight="1"

this is the final XML

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


    <TextView
        android:id="@+id/profileNickname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:scrollHorizontally="false"
        android:text="No Nickname yet fdg d trdt rdrt tdr  td trd t"
        android:textColor="#FFF"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/btnEditNickname"
        android:layout_width="42dp"
        android:layout_height="42dp"
        android:layout_marginLeft="8dp"
        android:layout_gravity="center"
        android:padding="6dp"
        android:scaleType="fitXY"
        app:srcCompat="@android:drawable/ic_menu_edit" />
</LinearLayout>

Try this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="horizontal">

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

        <TextView
            android:id="@+id/profileNickname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:scrollHorizontally="false"
            android:text="No Nickname yet fdg d trdt rdrt tdr  td trd t"
            android:textColor="#FFFFFF"
            android:textSize="24sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/btnEditNickname"
            android:layout_width="42dp"
            android:layout_height="42dp"
            android:layout_marginLeft="8dp"
            android:padding="6dp"
            android:scaleType="fitXY"
            app:srcCompat="@android:drawable/ic_menu_edit" />
    </LinearLayout>
</LinearLayout>

It'll give you the following result:

在此处输入图片说明

And don't forget to change the text color of the textView to that you want.

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