我需要创建一个如下所示的布局:
TextView
的内容应换行,以使ImageView
始终位于文本的开头。 TextView
不应与其他视图重叠,无论内容如何。ImageView
在运行时可以看到GONE
,因此TextView
应该使用ImageView
的空间。我当前的布局:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@android:color/white">
<TextView
android:id="@+id/pref_list_item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginStart="16dp"
android:fontFamily="sans-serif"
android:text="Long long"
android:textSize="@dimen/preference_text_size" />
<ImageView
android:id="@+id/pref_list_item_help_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/pref_list_item_title"
android:layout_centerVertical="true"
android:layout_margin="8dp"
android:src="@drawable/ic_help_primary_color"
android:contentDescription="@string/help" />
<Switch
android:id="@+id/pref_list_item_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="16dp" />
</RelativeLayout>
ConstraintLayout
是一个选择,但我无法创建满足我需求的约束。
答案 0 :(得分:2)
使用ConstraintLayout
可以这样:
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/pref_list_item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:fontFamily="sans-serif"
android:text="Long long asdasd asd asd aasdadasd"
android:textSize="@dimen/preference_text_size"
app:layout_constrainedWidth="true"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/pref_list_item_help_button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<ImageView
android:id="@+id/pref_list_item_help_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/pref_list_item_title"
android:layout_margin="8dp"
android:src="@drawable/ic_help_primary_color"
android:contentDescription="@string/help"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/pref_list_item_switch"
app:layout_constraintStart_toEndOf="@id/pref_list_item_title"
app:layout_constraintTop_toTopOf="parent"/>
<Switch
android:id="@+id/pref_list_item_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>
TextView
和ImageView
以packed
样式和0
的水平偏置链接在一起,以使其与左侧对齐。必须为app:layout_constrainedWidth="true"
设置TextView
,以防止在文本过长的情况下它与其他Views
重叠。当您想切换ImageView's
可见性时,所有这一切也很好用。