所以我想有一个标题旁边有两个按钮。这些按钮应该贴在文本旁边的左边,当文本太长时,它应该在两行上流动。
我可以通过给textview一个maxwidth来复制它,但是这会导致textview采用maxwidth,即使它在两行上重新流动。因此,我的按钮不再与文本对齐。
这样地:
如何使我的textview获得所需的宽度,而不是我告诉它使用maxwidth的宽度?
最佳答案
下面是使用constraintlayout的完美解决方案(因为我喜欢这个布局)。
这是密码。
<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" >
<android.support.constraint.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text"
android:layout_marginTop="25dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:maxWidth="300dp">
<TextView
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:maxWidth="300dp"
android:text="Some layout is going to be created and whatever I do it won't cross the limit."
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"/>
</android.support.constraint.ConstraintLayout>
<ImageView
android:layout_height="40dp"
android:background="#365987"
android:layout_width="40dp"
android:layout_marginStart="5dp"
android:id="@+id/image"
app:layout_constraintLeft_toRightOf="@+id/text"
app:layout_constraintTop_toTopOf="@+id/text"/>
<ImageView
android:id="@+id/image1"
android:layout_height="40dp"
android:background="#e85f11"
android:layout_width="60dp"
android:layout_marginStart="5dp"
app:layout_constraintBottom_toBottomOf="@+id/image"
app:layout_constraintLeft_toRightOf="@+id/image"
app:layout_constraintTop_toTopOf="@+id/image"/>
</android.support.constraint.ConstraintLayout>
输出:
不管文本是什么,它都不会超过maxwidth。如果宽度小于maxwidth,它的宽度将小于cc>。