[英]Android ConstraintLayout TextView go off the screen
As you see in the picture the text is off the wall. 如图所示,文字不在墙上。 This is such a bizarre behavior and I think it´sa bug 这是一个奇怪的行为,我认为这是一个错误
Here´sa screen shoot: 这是一个屏幕截图:
Here´s the header xml: 这是标题xml:
<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:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
android:orientation="vertical">
<TextView
android:id="@+id/street_name"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginStart="15dp"
android:text="street name"
android:textColor="?AppTheme.InformationText"
android:textSize="14sp"
android:textStyle="bold"
android:gravity="center"
android:layout_marginTop="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="19dp"
android:layout_marginLeft="0dp"
app:layout_constraintWidth_default="wrap"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="@+id/guideline29"
app:layout_constraintTop_toTopOf="@+id/guideline37"
app:layout_constraintRight_toLeftOf="@+id/guideline30"
app:layout_constraintHorizontal_bias="0.605"
app:layout_constraintVertical_bias="0.0"/>
<ImageView
android:id="@+id/street_view_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:padding="6dp"
android:scaleType="fitCenter"
tools:layout_constraintLeft_creator="1"
android:layout_marginLeft="0dp"
app:layout_constraintLeft_toLeftOf="@+id/country_flag_image"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="0dp"
app:layout_constraintRight_toLeftOf="@+id/guideline29"
android:layout_marginRight="8dp"
app:layout_constraintTop_toTopOf="@+id/guideline37"
android:layout_marginTop="8dp"/>
<ImageView
android:id="@+id/country_flag_image"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginLeft="4dp"
android:layout_marginStart="4dp"
android:layout_marginTop="8dp"
android:adjustViewBounds="true"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintTop_creator="1"
/>
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/exit_image"
android:layout_width="46dp"
android:layout_height="46dp"
android:layout_marginBottom="8dp"
android:layout_marginRight="-4dp"
android:src="@drawable/exit_custom"
app:border_color="#FF000000"
app:layout_constraintBottom_toBottomOf="@+id/street_view_image"
app:layout_constraintRight_toRightOf="@+id/street_view_image"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintRight_creator="1"
/>
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/guideline29"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5"/>
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/guideline30"
app:layout_constraintGuide_begin="395dp"
android:orientation="vertical"/>
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/guideline37"
app:layout_constraintGuide_begin="8dp"
android:orientation="horizontal"/>
</android.support.constraint.ConstraintLayout>
How can I make the TextView
called street_name
text not go outside screen? 如何才能使名为street_name
文本的TextView
不在屏幕外? I use the MaterialDrawer but I dont think that has anything to do with this. 我使用MaterialDrawer,但我不认为这与此有任何关系。 Device is samsung Note 4 having Android 6.0.1. 设备是三星Note 4有Android 6.0.1。 Using ConstraintLayout version com.android.support.constraint:constraint-layout:1.0.2
in Android Studio 2.3.3 在Android Studio 2.3.3中使用ConstraintLayout版本com.android.support.constraint:constraint-layout:1.0.2
Set android:layout_width="0dp"
for street_name
. 为street_name
设置android:layout_width="0dp"
。 That will cause it to match constraints and should keep it in bounds. 这将使它匹配约束并应将其保持在边界内。
Not working? 不工作? street_name
is constrained on the right by guideline30
at 395dp
. street_name
被限制在右侧guideline30
在395dp
。 Is 395dp
off the right of your screen? 屏幕右侧是395dp
吗?
Add the layconstraintEnd_toEndOf 添加layconstraintEnd_toEndOf
app:layout_constraintEnd_toEndOf="parent" 应用程式:layout_constraintEnd_toEndOf = “父”
and width 0dp 和宽度0dp
android:layout_width="0dp" 机器人:layout_width = “0dp”
Setting width 0dp alone won't work 单独设置宽度0dp将不起作用
Use app:layout_constraintEnd_toEndOf
instead of app:layout_constraintRight_toRightOf
Also beacuse Right_otRight may display wrong in some situation 使用app:layout_constraintEnd_toEndOf
而不是app:layout_constraintRight_toRightOf
也是因为在某些情况下Right_otRight可能显示错误
And use Start_toStartOf
instead of Left_toLeft
并使用Start_toStartOf
而不是Left_toLeft
Also like the others said set android:layout_width="0dp"
0dp
means match constraint 也像其他人说的设置android:layout_width="0dp"
0dp
表示匹配约束
For your <TextView android:id="@+id/street_name"...>
you should use android:layout_width="0dp"
and android:layout_height="wrap_content"
so that it looks like: 对于你的<TextView android:id="@+id/street_name"...>
你应该使用android:layout_width="0dp"
和android:layout_height="wrap_content"
这样它看起来像:
<TextView
android:id="@+id/street_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:text="street name"
android:textColor="?AppTheme.InformationText"
android:textSize="14sp"
android:textStyle="bold"
android:gravity="center"
android:layout_marginTop="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="19dp"
android:layout_marginLeft="0dp"
app:layout_constraintWidth_default="wrap"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="@+id/guideline29"
app:layout_constraintTop_toTopOf="@+id/guideline37"
app:layout_constraintRight_toLeftOf="@+id/guideline30"
app:layout_constraintHorizontal_bias="0.605"
app:layout_constraintVertical_bias="0.0"/>
EDIT #1: I guess you should constraint right side of your TextView by parent, not by guideline, ie change app:layout_constraintRight_toLeftOf="@+id/guideline30"
to app:layout_constraintRight_toRightOf="parent"
. 编辑#1:我猜你应该约束你的TextView的右侧,而不是指南,即更改app:layout_constraintRight_toLeftOf="@+id/guideline30"
到app:layout_constraintRight_toRightOf="parent"
。 As a result you have: 结果你有:
<TextView
android:id="@+id/street_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:text="street name"
android:textColor="?AppTheme.InformationText"
android:textSize="14sp"
android:textStyle="bold"
android:gravity="center"
android:layout_marginTop="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="19dp"
android:layout_marginLeft="0dp"
app:layout_constraintWidth_default="wrap"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="@+id/guideline29"
app:layout_constraintTop_toTopOf="@+id/guideline37"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.605"
app:layout_constraintVertical_bias="0.0"/>
Just attach TextView to right side and all be ok. 只需将TextView附加到右侧即可。 When you use a constraintLayout better practice to attach view to another view or parent, all 4 constraints should be used. 当您使用constraintLayout更好的做法将视图附加到另一个视图或父视图时,应使用所有4个约束。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.