我正在使用 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
是 wrapContent
。
app:layout_constraintLeft_toLeftOf="parent"
最佳答案
您可以将 TextView 的
宽度设置为 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"
关于android - TextView 使用 constraintLayout 离开屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53994248/