Constraint Layout android中正在剪切文本

时间:2017-05-04 06:42:28

标签: android android-layout android-constraintlayout

我使用下面的布局来实现约束布局右端的图像视图和图像左侧的文本:

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

我附上了从上面的XML获取的UI的屏幕截图。当文本太长时,文本将被剪切在左侧。

Screenshot_1

Screenshot_2

build.gradle中使用的依赖项是:

编译&#39; com.android.support.constraint:constraint-layout:1.0.1&#39;

3 个答案:

答案 0 :(得分:0)

您必须将LinearLayout更改为ConstraintLayout。 您的布局应如下所示。

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

Output:

答案 1 :(得分:0)

问题在于您在imageView上设置了固定的宽度和高度(android:layout_width="64dp"android:layout_height="64dp")。

您可以使用guidelines轻松约束imageView  赋予它相对于屏幕的尺寸。

您需要做的就是解决约束:

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

答案 2 :(得分:0)

简单地添加

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

*您可以根据需要随意更改marginLeft。