ConstraintLayout使用app:layout_constrainedHeight ="true"隐藏TextView的最后一行; [英] ConstraintLayout hides the last line of TextView with app:layout_constrainedHeight="true"
问题描述
我注意到ConstraintLayout
(版本1.1.3)的奇怪行为,每当我尝试使用带有wrap_content
属性的高度并将layout_constrainedHeight
设置为true
时,该行为都会隐藏TextView
的最后一行./p>
使用layout_constrainedHeight
:
没有layout_constrainedHeight
:
源代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="Lorem..."
app:layout_constrainedHeight="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
我以为,每当我想将wrap_content
与ConstraintLayout
一起使用时,都必须将layout_constrainedHeight
设置为true,但这有时会给我带来怪异的错误.我想念什么吗?
编辑
如果我删除了TextView
周围的边距,则可以正常工作.似乎ConstraintLayout
在wrap_content
和边距处做错了.
根据
app:layout_constrainedHeight =" true | false" 我将强调以下一段话:
保持强制约束以限制结果尺寸 因此,基本上,将 这里是 布局: 我的猜测是您可能不需要使用 看起来您想要的行为只有在没有 我已经更新了XML代码并录制了一些视频,以了解两者之间的区别. I have noticed weird behavior of With With no Source code: I thought that whenever I want to use EDIT If I remove the margin around According to documentation: WRAP_CONTENT : enforcing constraints (Added in 1.1) If a dimension is set to WRAP_CONTENT, in versions before 1.1 they will be treated as a literal dimension -- meaning, constraints will not limit the resulting dimension.
While in general this is enough (and faster), in some situations, you might want to use WRAP_CONTENT, yet keep enforcing constraints to limit the resulting dimension. In that case, you can add one of the corresponding attribute: app:layout_constrainedWidth="true|false" app:layout_constrainedHeight="true|false" I'll emphasise a piece of this quote:
keep enforcing constraints to limit the resulting dimension So basically, setting Here is an example with The layout: My guess is that you probably do not need to use Looks like the behaviour that you want is achievable only without the I've updated the XML code and recorded a little video to see the difference. 这篇关于ConstraintLayout使用app:layout_constrainedHeight ="true"隐藏TextView的最后一行;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!layout_constrainedHeight
或layout_constrainedWidth
设置为true
将保留约束并按指定的边距减小视图大小,而不是四处推挤其他所有视图并增加当前视图的高度/宽度以适合内容./p>
app:layout_constrainedHeight="true"
和app:layout_constrainedWidth=true
且边距不同的示例.红色TextView
包装内容,然后减小尺寸.绿色TextView
没有设置app:layout_constrained...="true"
属性和边距.它们的高度相等,但最终宽度不同.<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/top_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="86dp"
android:layout_marginEnd="26dp"
android:background="#ff2356"
android:text="@string/lorem_kind_of"
android:textColor="@android:color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constrainedHeight="true"/> <!-- This line is the only difference -->
<TextView
android:id="@+id/bottom_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="86dp"
android:layout_marginEnd="26dp"
android:background="#009900"
android:text="@string/lorem_kind_of"
android:textColor="@android:color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/top_text_view" />
</androidx.constraintlayout.widget.ConstraintLayout>
app:layout_constrainedHeight
属性.欢迎您发表评论,如果我的回答不能解决您的问题,我们将进一步详细说明.更新(2020年5月22日)
app:layout_constrainedHeight="true"
的情况下才能实现.我可能是错的,它取决于最终的期望结果,但是根据我的实验",看来app:layout_constrainedHeight
约束限制了视图的最小尺寸. ConstraintLayout
(version 1.1.3) that hides the last line of TextView
whenever I try to use height with wrap_content
property and layout_constrainedHeight
is set to true
.layout_constrainedHeight
:layout_constrainedHeight
:<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="Lorem..."
app:layout_constrainedHeight="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
wrap_content
with ConstraintLayout
I have to set layout_constrainedHeight
to true, but this sometimes gives me weird bugs. Am I missing something?TextView
, it works fine. It seems that ConstraintLayout
does something wrong with wrap_content
and margins.
layout_constrainedHeight
or layout_constrainedWidth
to true
will preserve constraints and reduce view size by the specified margin instead of pushing all the other views around and increasing current view height/width to fit the content.app:layout_constrainedHeight="true"
and app:layout_constrainedWidth="true"
and different margins. Red TextView
wrapped it's content and after that was reduced in size. Green TextView
has no app:layout_constrained...="true"
attributes and margins set. Their height is equal, but the width is different in the end.<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/top_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="86dp"
android:layout_marginEnd="26dp"
android:background="#ff2356"
android:text="@string/lorem_kind_of"
android:textColor="@android:color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constrainedHeight="true"/> <!-- This line is the only difference -->
<TextView
android:id="@+id/bottom_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="86dp"
android:layout_marginEnd="26dp"
android:background="#009900"
android:text="@string/lorem_kind_of"
android:textColor="@android:color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/top_text_view" />
</androidx.constraintlayout.widget.ConstraintLayout>
app:layout_constrainedHeight
attribute. You are welcome to leave a comment and we will elaborate on this further if my answer doesn't solve your problem.UPDATE (22 May 2020)
app:layout_constrainedHeight="true"
. I may be wrong, it depends on the final desired result, but according to my "experiments" looks like app:layout_constrainedHeight
constraints a view from growing further it's minimum size.