无法找出Android中的布局依存关系

时间:2018-10-02 18:08:55

标签: android xml layout android-constraintlayout

我需要创建一个如下所示的布局:

  1. TextView的内容应换行,以使ImageView始终位于文本的开头。
  2. TextView不应与其他视图重叠,无论内容如何。
  3. 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是一个选择,但我无法创建满足我需求的约束。

1 个答案:

答案 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>

TextViewImageViewpacked样式和0的水平偏置链接在一起,以使其与左侧对齐。必须为app:layout_constrainedWidth="true"设置TextView,以防止在文本过长的情况下它与其他Views重叠。当您想切换ImageView's可见性时,所有这一切也很好用。