我有三个视图,分别是标题、版本和ImageView(作为按钮):
- 标题应该是
wrap_content
,但要遵守以下规则 - 版本应该是
wrap_content
,在标题右侧且在ImageView左侧 - ImageView具有固定大小,位于父视图的右上角
问题在于,如果标题太大,版本号将向右移动,并且“版本号在ImageView左侧”规则将不被遵守:
因此,我需要限制标题宽度,使版本号可见且不向右移动。
这是XML:
<?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:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:background="#b3b2b2">
<!-- -->
<TextView
android:id="@+id/LibraryWithVersionItem.title"
android:layout_width="0dp"
android:textStyle="bold"
android:textSize="@dimen/fontSize18"
android:textColor="@color/mySecondaryDarkColor"
android:layout_height="wrap_content"
android:ellipsize="middle"
tools:text="ExampleLibrary 01234567890123456789012345"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
/>
<Spinner
android:id="@+id/LibraryWithVersionItem.versions"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:textSize="@dimen/fontSize16"
android:textColor="@color/mySecondaryDarkColor"
tools:listitem="@layout/library_version"
android:layout_marginTop="@dimen/margin8"
android:layout_marginLeft="@dimen/margin8"
android:layout_marginRight="@dimen/margin8"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="@+id/LibraryWithVersionItem.title"
app:layout_constraintRight_toLeftOf="@+id/LibraryWithVersionItem.info"
app:layout_constraintHorizontal_bias="0.0"/>
<TextView
android:id="@+id/LibraryWithVersionItem.sentence"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/LibraryWithVersionItem.title"
tools:text="Some library description in one sentence\nbut two lines"
android:layout_marginTop="@dimen/margin8"
android:layout_marginLeft="@dimen/margin8"
app:layout_constraintRight_toLeftOf="@+id/LibraryWithVersionItem.install"
android:layout_marginRight="8dp"
app:layout_constraintHorizontal_bias="0.0"/>
<TextView
android:id="@+id/LibraryWithVersionItem.isInstalled"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/LibraryManager.installed"
android:textColor="#1a7c1a"
android:layout_marginTop="@dimen/margin8"
android:layout_marginBottom="@dimen/margin8"
android:layout_marginLeft="@dimen/margin8"
android:layout_marginRight="@dimen/margin8"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/LibraryWithVersionItem.sentence"
app:layout_constraintRight_toLeftOf="@+id/LibraryWithVersionItem.install"
app:layout_constraintHorizontal_bias="0.0"/>
<!-- information button -->
<ImageView
android:id="@+id/LibraryWithVersionItem.info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="@dimen/margin8"
android:paddingLeft="@dimen/margin8"
android:paddingRight="@dimen/margin8"
android:paddingBottom="@dimen/margin8"
android:scaleType="center"
android:src="@drawable/ic_info_outline_white_24dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<!-- install button -->
<ImageView
android:id="@+id/LibraryWithVersionItem.install"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/margin8"
android:paddingRight="@dimen/margin8"
android:paddingTop="@dimen/margin8"
android:paddingBottom="@dimen/margin8"
android:scaleType="center"
android:src="@drawable/ic_get_app_white_24dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/LibraryWithVersionItem.info"/>
</android.support.constraint.ConstraintLayout>
PS 1. layout_width="0dp"
+ app:layout_constraintWidth_default="wrap"
看起来正是我所需要的("wrap_content但不破坏约束"), 但它并不起作用(仍然比所需的大):
<TextView
android:id="@+id/LibraryWithVersionItem.title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:ellipsize="middle"
android:textColor="@color/mySecondaryDarkColor"
android:textSize="@dimen/fontSize18"
android:textStyle="bold"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_default="wrap"
tools:text="ExampleLibrary 01234567890123456789012345"
PS 2. 设置版本的最小约束宽度(app:layout_constraintWidth_min="60dp"
)也没有帮助 - 它是不可见的,因为它被移动得太靠右了。
app:layout_constraintWidth_default
是一个属性,谢谢! - Oblivionkey3app:layout_constraintWidth_default="wrap"
和android:layout_width="0dp"
, 但是出现了控制台警告。 将其更改为android:layout_width="wrap_content"
并添加app:layout_constrainedWidth="true"
而不使用app:layout_constraintWidth_default
,它可以正常工作,没有控制台错误。 - Pieter van der Vyver