ConstraintLayout内部的Wrap_content视图延伸到屏幕外部 [英] Wrap_content view inside a ConstraintLayout stretches outside the screen

查看:459
本文介绍了ConstraintLayout内部的Wrap_content视图延伸到屏幕外部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ConstraintLayout实现一个简单的聊天气泡.这是我要实现的目标:

I am trying to implement a simple chat bubble using a ConstraintLayout. This is what I am trying to achieve:

但是,wrap_content似乎不能在约束条件下正常工作.它尊重边距,但不能正确计算可用空间.这是我的布局:

However, wrap_content seem not to work properly with constraints. It respects the margins, but doesn't calculate the available space properly. Here is my layout:

<?xml version="1.0" encoding="utf-8"?>
<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">

    <TextView
        android:id="@+id/chat_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintHorizontal_bias="0"
        tools:background="@drawable/chat_message_bubble"
        tools:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris sodales accumsan tortor at bibendum."
        android:layout_marginStart="64dp"
        android:layout_marginLeft="64dp"
        android:layout_marginEnd="32dp"
        android:layout_marginRight="32dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp" />
</android.support.constraint.ConstraintLayout>

呈现如下:

我正在使用com.android.support.constraint:constraint-layout:1.0.0-beta4.

我做错什么了吗?它是错误还是只是非直觉的行为?我可以使用ConstraintLayout来实现正确的行为吗(我知道我可以使用其他布局,我具体询问的是ConstrainLayout.)

Am I doing something wrong? Is it a bug or just an unintuitive behavior? Can I achieve the proper behavior using a ConstraintLayout (I know I can use other layouts, I am asking about ConstrainLayout specifically).

推荐答案

已过时:查看更好的答案

不,您不能像今天(1.0 beta 4)那样使用ConstraintLayout做您想做的事情:

Outdated: See better answer

No, you cannot do what you want with ConstraintLayout as it is today (1.0 beta 4):

  • wrap_content仅要求窗口小部件进行自我测量,但不会限制其扩展不受最终约束的影响
  • match_constraints(0dp)限制约束的大小...但是即使wrap_content较小(您的第一个示例),它也将匹配它们(这不是约束)也不是你想要的.
  • wrap_content only asks the widget to measure itself, but won't limit its expansion against eventual constraints
  • match_constraints (0dp) will limit the size of the widget against the constraints... but will match them even if wrap_content would have been smaller (your first example), which isn't what you want either.

所以现在,对于这种特殊情况,您很不走运:-/

So right now, you are out of luck for that particular case :-/

现在...我们正在考虑为match_constraints添加额外的功能来处理这种确切的情况(除非大小超过限制,否则表现为wrap_content.)

Now... we are thinking about adding extra capabilities to match_constraints to deal with this exact scenario (behaving as wrap_content unless the size ends being more than the constraints).

我不能保证这个新功能会在1.0版本之前实现.

I cannot promise that this new feature will make it before the 1.0 release though.

编辑:我们确实在1.0中添加了此功能,并添加了属性app:layout_constraintWidth_default="wrap"(宽度设置为0dp).如果设置,则该小部件将具有与使用wrap_content相同的大小,但是会受到约束的限制(即,它不会扩展到它们之外)

Edit: we did add this capability in 1.0 with the attribute app:layout_constraintWidth_default="wrap" (with width set to 0dp). If set, the widget will have the same size as if using wrap_content, but will be limited by constraints (i.e. it won't expand beyond them)

更新 现在这些标签已被弃用,取而代之的是使用layout_width ="WRAP_CONTENT"和layout_constrainedWidth ="true".

Update Now those tags are deprecated, instead use layout_width="WRAP_CONTENT" and layout_constrainedWidth="true".

这篇关于ConstraintLayout内部的Wrap_content视图延伸到屏幕外部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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