简体   繁体   中英

Text is being clipped off inside Constraint Layout android

I use the below layout to achieve a view with image on the right end of a constraint layout, and a text to the left of the image:

<LinearLayout
 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:orientation="vertical" 
         android:layout_width="match_parent"
         android:layout_height="match_parent">

  <android.support.constraint.ConstraintLayout             
    android:id="@+id/parentPanel"             
    android:layout_width="wrap_content"             
    android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/image"
                 android:layout_width="64dp"
                 android:layout_height="64dp"
                 app:layout_constraintRight_toRightOf="parent"
                 android:background="#ff0000"/>

            <TextView
                android:id="@+id/txt"
                android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:text="Lorem Ipsum is simply dummy text of the printing
 and typesetting industry. Lorem Ipsum has been the industry's
 standard dummy text ever since the 1500s, when an unknown printer took
 a galley of type and scrambled it to make a type specimen book"
                 app:layout_constraintTop_toTopOf="parent"
                 app:layout_constraintBottom_toBottomOf="parent"
                 app:layout_constraintRight_toLeftOf="@+id/image"
                 android:textColor="#000000"
                 android:textSize="20sp"/>
         </android.support.

constraint.ConstraintLayout>
     </LinearLayout>

I have attached the screenshot of the UI that has been obtained from above XML. The text is being clipped off on the left, when text is too long.

Screenshot_1

Screenshot_2

The dependency that is being used in build.gradle is:

compile 'com.android.support.constraint:constraint-layout:1.0.1'

The problem was with your fixed width and height on your imageView ( android:layout_width="64dp" and android:layout_height="64dp" ).

You can easily constraint you imageView using guidelines to give it size that relative to your screen.

All you need to do is fix your constraint :

<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:id="@+id/frameLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragments.MenusDesign.ExpandableCategoriesMenu.ExpandableCategoriesMenu">


<ImageView
    android:id="@+id/image"
    android:layout_width="64dp"
    android:layout_height="64dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:background="#ff0000"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/txt"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:text="Lorem Ipsum is simply dummy text of the printing
 and typesetting industry. Lorem Ipsum has been the industry's
 standard dummy text ever since the 1500s, when an unknown printer took
 a galley of type and scrambled it to make a type specimen book"
    android:textColor="#000000"
    android:textSize="20sp"
    app:layout_constraintEnd_toStartOf="@+id/image"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

You have to change LinearLayout to ConstraintLayout . Your Layout should like below.

<?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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/parentPanel"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" >

        <ImageView
            android:id="@+id/image"
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:background="#ff0000"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"  />

        <TextView
            android:id="@+id/txt"
            android:layout_width="0dp"
            android:layout_height="120dp"
            android:layout_marginStart="10dp"
            android:layout_marginEnd="10dp"
            android:text="Lorem Ipsum is simply dummy text of the printing
 and typesetting industry. Lorem Ipsum has been the industry's
 standard dummy text ever since the 1500s, when an unknown printer took
 a galley of type and scrambled it to make a type specimen book"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:textColor="#000000"
            android:textSize="20sp"
            app:layout_constraintVertical_bias="0.0"
            app:layout_constraintRight_toLeftOf="@+id/image"
            android:layout_marginLeft="0dp"
            android:layout_marginRight="8dp" />
    </android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>

输出:

Simply, add

app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="8dp"

*You can change the marginLeft as much as 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