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

查看:41
本文介绍了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 does not do what I want. It respects the margins, but expands outside of the view bounds. 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).

推荐答案

过时:查看更好的答案

不,你不能像今天一样用 ConstraintLayout 做你想做的事(1.0 beta 4):

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天全站免登陆