Mar*_*nak 107 android android-layout android-constraintlayout
我正在尝试使用一个简单的聊天气泡ConstraintLayout.这就是我想要实现的目标:
但是,wrap_content似乎没有与约束一起正常工作.它尊重边距,但不能正确计算可用空间.这是我的布局:
<?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>
Run Code Online (Sandbox Code Playgroud)
这呈现如下:
我在用com.android.support.constraint:constraint-layout:1.0.0-beta4.
难道我做错了什么?这是一个错误还是一个不直观的行为?ConstraintLayout我可以使用(我知道我可以使用其他布局,我ConstrainLayout具体询问)来实现正确的行为.
Nic*_*ard 207
不,你现在不能用ConstraintLayout做你想要的(1.0 beta 4):
wrap_content 仅要求窗口小部件自行测量,但不会限制其扩展以抵御最终约束match_constraints(0dp)将根据约束限制小部件的大小...但是即使它们更wrap_content小(你的第一个例子)也会匹配它们,这也不是你想要的.所以现在,你对这个特殊情况运气不好: - /
现在......我们正在考虑添加额外的功能来match_constraints处理这个确切的场景(表现为wrap_content除非大小超过约束).
我不能保证这个新功能会在1.0发布之前完成.
编辑:我们在1.0中使用属性app:layout_constraintWidth_default="wrap"(宽度设置为0dp)添加了此功能.如果设置,小部件将具有与使用wrap_content相同的大小,但将受到约束的限制(即它不会扩展超出它们)
Sil*_*a H 186
使用app:layout_constrainedWidth="true"宽度设置为android:layout_width="wrap_content"
app:layout_constraintWidth_default="wrap" 宽度设置为 android:layout_width="0dp"
Eug*_*sov 19
是的,正如Nikolas Roard给出的答案所述,你应该添加app:layout_constraintWidth_default="wrap"并设置宽度为0dp.要正确调整气泡,你应该设置1.0 layout_constraintHorizontal_bias.
这是最终的源代码:
<android.support.constraint.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/chat_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="16dp"
android:layout_marginTop="8dp"
android:layout_marginStart="64dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintWidth_default="wrap"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:background="@drawable/chat_message_bubble"
android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris sodales accumsan tortor at bibendum." />
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
结果看起来像:
就像已经说过的其他答案一样,自ConstraintLayout 1.0以来,它可以实现这一点,但是从最新版本(1.1.x)开始,它们已经改变了你的方式.
自ConstraintLayout 1.1发布以来,旧的app:layout_constraintWidth_default="wrap"和app:layout_constraintHeight_default="wrap"属性现已弃用.
如果您想提供wrap_content行为,但仍然强制执行View上的约束,则应将其宽度和/或高度设置为wrap_content与app:layout_constrainedWidth=”true|false”和/或app:layout_constrainedHeight=”true|false”属性结合使用,如文档中所述:
WRAP_CONTENT:强制约束(在1.1中添加)如果维度设置为WRAP_CONTENT,则在1.1之前的版本中,它们将被视为文字维度 - 这意味着约束不会限制生成的维度.虽然通常这足够(并且更快),但在某些情况下,您可能希望使用WRAP_CONTENT,但仍然强制执行约束以限制结果维度.在这种情况下,您可以添加一个相应的属性:
app:layout_constrainedWidth ="true | false"app:layout_constrainedHeight ="true | false"
至于最新版本,当我回答这个问题时,ConstraintLayout的版本是1.1.2.
@nicolas-roard 的回答app:layout_constraintWidth_default="wrap"和android:layout_width="0dp"现在已弃用。
继续使用app:layout_constrainedWidth="true"和android:layout_width="wrap_content"。
至于被淘汰的原因,我不知道。但它在ConstraintLayout的源代码中是正确的
| 归档时间: |
|
| 查看次数: |
44296 次 |
| 最近记录: |