Android ConstraintLayout—让元素占用必要的空间,直到';有空吗
我有一个文本视图和一个按钮,按顺序水平排列在ConstraintLayout中: 当文本足够短时,我需要第一个元素(TextView)仅占用必要的空间,但当需要显示更多文本时,需要根据需要进行扩展,同时仍需要为第二个元素(Button)留出足够的空间,以便在视图内完全渲染,其末端与父元素的末端对齐 这就是XML当前的样子:Android ConstraintLayout—让元素占用必要的空间,直到';有空吗,android,android-constraintlayout,Android,Android Constraintlayout,我有一个文本视图和一个按钮,按顺序水平排列在ConstraintLayout中: 当文本足够短时,我需要第一个元素(TextView)仅占用必要的空间,但当需要显示更多文本时,需要根据需要进行扩展,同时仍需要为第二个元素(Button)留出足够的空间,以便在视图内完全渲染,其末端与父元素的末端对齐 这就是XML当前的样子: <android.support.constraint.ConstraintLayout android:layout_width="match_parent
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp">
<TextView
android:id="@+id/element1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Short enough text"/>
<Button
android:id="@+id/element2"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_gravity="center_vertical"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:drawableLeft="@drawable/element2ButtonDrawable"
android:drawablePadding="0dp"
android:drawableStart="@drawable/element2ButtonDrawable"
android:text="Action"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@id/element1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.0"/>
</android.support.constraint.ConstraintLayout>
这是当从“足够短的文本”切换到“将导致大部分底部被推到父视图边界之外的较长文本”时树的呈现方式:
是否可以通过使用ConstraintLayout来实现我想要做的事情
(撰写本文时,最新版本为1.0.2)
谢谢 试试这样的方法:
<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:layout_margin="16dp">
<TextView
android:id="@+id/element1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/element2"
app:layout_constraintTop_toTopOf="parent"
tools:text="A longer text that will cause most of the bottom to get pushed outside of the parent view bounds" />
<Button
android:id="@+id/element2"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:text="Action"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
您应该在文本视图中使用压缩水平链,其宽度与约束匹配,水平偏差等于0.0 以下是解决方案:
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp">
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:text="Short enough text"
app:layout_constraintWidth_default="wrap"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintHorizontal_bias="0.0" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:drawablePadding="0dp"
android:drawableStart="@drawable/element2buttondrawable"
android:text="Action"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="@+id/textView"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</android.support.constraint.ConstraintLayout>
您可以在以下文章中阅读有关使用链的更多信息:
TextView有android:maxLength=“maxNumberOfChars”,但这不适用于不同的屏幕大小是的,这是我当前的解决方法,正是我当前的问题><谢谢!你有密码吗。?