【发布时间】:2017-04-12 13:38:54
【问题描述】:
我正在尝试使用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>
呈现如下:
我正在使用com.android.support.constraint:constraint-layout:1.0.0-beta4。
我做错了吗?这是一个错误还是只是一个不直观的行为?我可以使用ConstraintLayout 实现正确的行为吗(我知道我可以使用其他布局,我特别询问ConstrainLayout)。
【问题讨论】:
-
你能把文本视图连同它的父约束布局一起发布吗?如您所知,父布局的属性对子布局的影响很大
-
顺便说一下,在你的情况下,我猜水平偏差是罪魁祸首。尝试删除 layoutright to right of 和 bias
-
水平偏差是必要的,否则如果气泡居中。如果没有从右到右的布局,则不会考虑右边距,这不是我们想要的。我按照您的建议尝试删除它们,但没有帮助。
-
问题肯定是水平偏差 0。我将检查可能的解决方案并尽快发布,因为我也在约束布局上使用类似的东西。
-
@nmu 聊天气泡来自
tools:background="@drawable/chat_message_bubble"。要实现它,您必须在 drawable 文件夹中创建 chat_message_bubble.xml 文件,然后添加以下代码:<shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#FB4382"/> <corners android:radius="10dip"/> </shape>
标签: android android-layout android-constraintlayout