TextView выходит из экрана с constraintLayout - PullRequest
0 голосов
/ 01 января 2019

Я использую ConstraintLayout для создания ниже XML в моем приложении.Как вы можете видеть, мой первый предмет в порядке, но во втором некоторые части моего textView не отображаются на экране!

Это мой XML код:

<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/ly_user"
        android:padding="8dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <ImageView
            app:layout_constraintRight_toRightOf="parent"
            android:id="@+id/im_user_icon"
            android:layout_width="@dimen/default_message_icon_size"
            android:layout_height="@dimen/default_message_icon_size"
            android:src="@drawable/user_pacific"/>

    <ImageView
            app:layout_constraintTop_toBottomOf="@+id/im_user_icon"
            app:layout_constraintRight_toLeftOf="@+id/im_user_icon"
            android:id="@+id/im_user_arrow"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:src="@drawable/arrow_bg1"/>

    <View
            android:id="@+id/view_user_guidLine"
            android:layout_width="1dp"
            android:layout_height="0dp"
            app:layout_constraintLeft_toLeftOf="@id/im_user_arrow"
            app:layout_constraintRight_toRightOf="@id/im_user_arrow"/>

    <TextView
            app:layout_constraintTop_toBottomOf="@+id/im_user_icon"
            app:layout_constraintRight_toLeftOf="@+id/view_user_guidLine"
            android:id="@+id/tv_user_message"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:padding="8dp"
            android:minWidth="@dimen/default_message_textarea_width"
            android:minHeight="@dimen/default_message_textarea_height"
            android:background="@drawable/bg_right_text"
            android:textColor="@android:color/white"/>


</android.support.constraint.ConstraintLayout>

Я знаю, что могу решить эту проблему, добавив строку ниже к textView, но мне нужен мой textView be wrapContent.

 app:layout_constraintLeft_toLeftOf="parent"

enter image description here

Ответы [ 3 ]

0 голосов
/ 01 января 2019

Вы можете установить ширину TextView's на wrap_content, но, чтобы предотвратить ее расширение за пределы экрана, вам также необходимо добавить левое ограничение и использовать app:layout_constrainedWidth="true" для применения ограничений.

Теперь, чтобы заставить TextView придерживаться вправо, вам нужно добавить app:layout_constraintHorizontal_bias="1", который выровняет его по правому ограничению.

Итак, в целом, это изменения, необходимые для TextView:

app:layout_constraintLeft_toLeftOf="parent"
app:layout_constrainedWidth="true"
app:layout_constraintHorizontal_bias="1"
android:layout_width="wrap_content"
0 голосов
/ 20 августа 2019

Принятый ответ работает, но на моей стороне я использую android:layout_width="0dp", потому что мой TextView находится в середине двух элементов.

<TextView
        android:id="@+id/my_id_text_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/long_text"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/second_vertical_divider_view"
        app:layout_constraintStart_toEndOf="@+id/first_vertical_divider_view"
        app:layout_constraintTop_toTopOf="parent" />
0 голосов
/ 01 января 2019

Добавьте следующую строку в свой код в TextView

app:layout_constraintWidth_percent="0.6"

в первой строке layout_constraintWidth_percent - это 60 процентов ширины экрана вашего телефона, которую вы можете изменить в соответствии со своими потребностями.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...