[英]TextView going out of screen with constraintLayout
I am using ConstraintLayout
to create below xml in my application.我正在使用
ConstraintLayout
在我的应用程序中创建下面的 xml。 As you can see my first item is fine, But in the second one, some parts of my textView
are not on the screen!如您所见,我的第一项很好,但是在第二项中,我的
textView
的某些部分不在屏幕上!
This is my XML
code:这是我的
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>
I Know I can fix this problem to adding below line to the textView
, But I need my textView
be wrapContent
.我知道我可以解决这个问题,将下面的行添加到
textView
,但我需要我的textView
是wrapContent
。
app:layout_constraintLeft_toLeftOf="parent"
You can set your TextView's
width to wrap_content
, but to prevent it from expanding outside of screen you need to add the left constraint as well and use app:layout_constrainedWidth="true"
to enforce constraints. 您可以将
TextView's
宽度设置为wrap_content
,但是要防止其扩展到屏幕之外,您还需要添加左侧约束,并使用app:layout_constrainedWidth="true"
来实施约束。
Now, to make the TextView
stick to the right, you need to add app:layout_constraintHorizontal_bias="1"
, which will align it to its right constraint. 现在,要使
TextView
靠右,您需要添加app:layout_constraintHorizontal_bias="1"
,它将使其与右约束对齐。
So all in all, these are the changes needed for the TextView
: 因此,总而言之,这些都是
TextView
所需的更改:
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constrainedWidth="true"
app:layout_constraintHorizontal_bias="1"
android:layout_width="wrap_content"
Add these following line in your code in your TextView
在您的
TextView
代码中添加以下这些行
app:layout_constraintWidth_percent="0.6"
first line layout_constraintWidth_percent
is 60 percent of your phone screen width you can change it according to your need. 第一行
layout_constraintWidth_percent
是手机屏幕宽度的60%,您可以根据需要进行更改。
Accepted answered works but on my side I use android:layout_width="0dp"
because my TextView is in the middle of two elements. 可以接受的答案有效,但在我这边,我使用
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" />
Accepted answer didn't helped in my case.接受的答案对我来说没有帮助。 Alternative solution can look like that:
替代解决方案可能如下所示:
<ConstraintLayout>
<TextView
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
app:layout_constraintEnd_toStartOf="@+id/option_info"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_max="wrap" />
<ImageView
android:id="@+id/option_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_info"
app:layout_constraintBottom_toBottomOf="@+id/title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/title"
app:layout_constraintTop_toTopOf="@+id/title" />
</ConstraintLayout>
The idea is to prevent one view pushing another while expanding by creating a chain and add some constraint params to hold influenced view in view's border.这个想法是通过创建一个链来防止一个视图在扩展时推动另一个视图,并添加一些约束参数以将受影响的视图保存在视图的边界中。 Hope it still will help somebody!
希望它仍然会帮助别人!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.