ConstraintLayout使用app:layout_constrainedHeight ="true"隐藏TextView的最后一行; [英] ConstraintLayout hides the last line of TextView with app:layout_constrainedHeight="true"

查看:74
本文介绍了ConstraintLayout使用app:layout_constrainedHeight ="true"隐藏TextView的最后一行;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到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_contentConstraintLayout一起使用时,都必须将layout_constrainedHeight设置为true,但这有时会给我带来怪异的错误.我想念什么吗?

编辑

如果我删除了TextView周围的边距,则可以正常工作.似乎ConstraintLayoutwrap_content和边距处做错了.

解决方案

根据

app:layout_constrainedHeight =" true | false"

我将强调以下一段话: 保持强制约束以限制结果尺寸

因此,基本上,将layout_constrainedHeightlayout_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约束限制了视图的最小尺寸.

我已经更新了XML代码并录制了一些视频,以了解两者之间的区别.

I have noticed weird behavior of 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.

With layout_constrainedHeight:

With no layout_constrainedHeight:

Source code:

<?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>

I thought that whenever I want to use wrap_content with ConstraintLayout I have to set layout_constrainedHeight to true, but this sometimes gives me weird bugs. Am I missing something?

EDIT

If I remove the margin around TextView, it works fine. It seems that ConstraintLayout does something wrong with wrap_content and margins.

解决方案

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 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.

Here is an example with 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.

The layout:

<?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>

My guess is that you probably do not need to use 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)

Looks like the behaviour that you want is achievable only without the 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.

I've updated the XML code and recorded a little video to see the difference.

这篇关于ConstraintLayout使用app:layout_constrainedHeight ="true"隐藏TextView的最后一行;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆