使用 Constraints 而不是 maxWidth 属性来限制宽度
Using Constraints instead of the maxWidth attribute for width limits
我想知道是否有可能实现以下(消息线程;首先从发件人的右侧开始,而不是收件人的一侧)视图:
...使用支持库中的 Android 约束而不是 maxWidth 属性,特别是文本气泡的宽度 - 我非常想要容器的宽度(由 TextView 和它的圆形背景和发件人的 ImageView 图标)宽度最多为屏幕的五分之四,然后像标准消息传递应用程序一样相应地包裹下面的以下文本行。因为截至目前,我无法使用如下约束指南实现它:
<?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">
<android.support.constraint.Guideline
android:id="@+id/left_margin_guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.2"/>
<android.support.constraint.ConstraintLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="@dimen/padding_one_half"
app:layout_constraintStart_toEndOf="@+id/left_margin_guideline"
app:layout_constraintEnd_toEndOf="parent">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/message_icon"
android:layout_width="@dimen/conversation_icon_size"
android:layout_height="@dimen/conversation_icon_size"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
tools:src="@drawable/ic_toolbar_logo"/>
<io.github.rockerhieu.emojicon.EmojiconTextView
android:id="@+id/message_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_curved_corners_message"
android:padding="@dimen/padding_one_half"
android:layout_marginRight="@dimen/padding_one_half"
android:layout_marginEnd="@dimen/padding_one_half"
app:layout_constraintEnd_toStartOf="@+id/message_icon"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
tools:text="This is a message that should wrap if it gets too long or if there is a \nnewline in the middle of the message"/>
<TextView
android:id="@+id/message_posted"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/font_small"
android:textColor="@color/colorGrayXLight"
android:layout_marginTop="@dimen/padding_one_half"
app:layout_constraintTop_toBottomOf="@+id/message_text"
app:layout_constraintEnd_toEndOf="@+id/message_text"
tools:text="2:45 PM"/>
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
... 结果是:
正如您所注意到的,消息文本从一开始就被截断了,如果我用父级设置文本的开始约束,那么单行的整个目的就落空了文本(即单个单词),因为文本的宽度将与父文本匹配,当我希望它像顶部屏幕截图中的那样被包裹时,文本居中。
综上所述,如果我能在有约束的情况下解决这个问题就好了,因为我希望这个布局在不使用 maxWidth 属性的情况下在其他屏幕尺寸上尽可能地响应使用硬编码值或根据屏幕大小以编程方式设置它,我发现这在样板代码方面非常不方便,尤其是对于消息线程视图。
是的,您可以在该文本元素(使用 ConstraintLayout 1.1)上使用偏置居中约束(左 + 右 + 偏置 1)和 constrainedWidth 来实现:
<?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/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="32dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:text="a message that should wrap if it is too long or if there is a space in the middle of the text"
app:layout_constrainedWidth="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/imageView2"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/ic_launcher_round" />
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="2:45 PM"
app:layout_constraintEnd_toStartOf="@+id/imageView2"
app:layout_constraintTop_toBottomOf="@+id/textView7" />
</android.support.constraint.ConstraintLayout>
用它正确适应的较短消息替换文本:
解决此类问题的正确方法是根据需要使用app:layout_constrainedWidth=”true|false”
或app:layout_constrainedHeight=”true|false”
。
我想知道是否有可能实现以下(消息线程;首先从发件人的右侧开始,而不是收件人的一侧)视图:
...使用支持库中的 Android 约束而不是 maxWidth 属性,特别是文本气泡的宽度 - 我非常想要容器的宽度(由 TextView 和它的圆形背景和发件人的 ImageView 图标)宽度最多为屏幕的五分之四,然后像标准消息传递应用程序一样相应地包裹下面的以下文本行。因为截至目前,我无法使用如下约束指南实现它:
<?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">
<android.support.constraint.Guideline
android:id="@+id/left_margin_guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.2"/>
<android.support.constraint.ConstraintLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="@dimen/padding_one_half"
app:layout_constraintStart_toEndOf="@+id/left_margin_guideline"
app:layout_constraintEnd_toEndOf="parent">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/message_icon"
android:layout_width="@dimen/conversation_icon_size"
android:layout_height="@dimen/conversation_icon_size"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
tools:src="@drawable/ic_toolbar_logo"/>
<io.github.rockerhieu.emojicon.EmojiconTextView
android:id="@+id/message_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_curved_corners_message"
android:padding="@dimen/padding_one_half"
android:layout_marginRight="@dimen/padding_one_half"
android:layout_marginEnd="@dimen/padding_one_half"
app:layout_constraintEnd_toStartOf="@+id/message_icon"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
tools:text="This is a message that should wrap if it gets too long or if there is a \nnewline in the middle of the message"/>
<TextView
android:id="@+id/message_posted"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/font_small"
android:textColor="@color/colorGrayXLight"
android:layout_marginTop="@dimen/padding_one_half"
app:layout_constraintTop_toBottomOf="@+id/message_text"
app:layout_constraintEnd_toEndOf="@+id/message_text"
tools:text="2:45 PM"/>
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
... 结果是:
正如您所注意到的,消息文本从一开始就被截断了,如果我用父级设置文本的开始约束,那么单行的整个目的就落空了文本(即单个单词),因为文本的宽度将与父文本匹配,当我希望它像顶部屏幕截图中的那样被包裹时,文本居中。
综上所述,如果我能在有约束的情况下解决这个问题就好了,因为我希望这个布局在不使用 maxWidth 属性的情况下在其他屏幕尺寸上尽可能地响应使用硬编码值或根据屏幕大小以编程方式设置它,我发现这在样板代码方面非常不方便,尤其是对于消息线程视图。
是的,您可以在该文本元素(使用 ConstraintLayout 1.1)上使用偏置居中约束(左 + 右 + 偏置 1)和 constrainedWidth 来实现:
<?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/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="32dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:text="a message that should wrap if it is too long or if there is a space in the middle of the text"
app:layout_constrainedWidth="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/imageView2"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/ic_launcher_round" />
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="2:45 PM"
app:layout_constraintEnd_toStartOf="@+id/imageView2"
app:layout_constraintTop_toBottomOf="@+id/textView7" />
</android.support.constraint.ConstraintLayout>
用它正确适应的较短消息替换文本:
解决此类问题的正确方法是根据需要使用app:layout_constrainedWidth=”true|false”
或app:layout_constrainedHeight=”true|false”
。