【问题标题】:Constraint layout chain packed usage约束布局链打包使用
【发布时间】:2016-09-30 15:26:39
【问题描述】:

我正在尝试使用约束布局链将两个文本视图垂直居中。我知道我可以将文本布局包装在线性布局中,但我想尝试新的链功能。当我构建我的项目时,虽然我得到一个错误,Error:(28, 50) No resource found that matches the given name (at 'layout_constraintBottom_toTopOf' with value '@id/item_school_location').

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="72dp">

        <ImageView
            android:id="@+id/item_school_logo"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_marginStart="16dp"
            android:contentDescription="@string/item_school_logo_description"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.5" />

        <TextView
            android:id="@+id/item_school_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:layout_marginStart="72dp"
            android:maxLines="1"
            android:textAppearance="@style/TextAppearance.AppCompat.Light.SearchResult.Subtitle"
            app:layout_constraintBottom_toTopOf="@id/item_school_location"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.5"
            app:layout_constraintVertical_chainPacked="true" />

        <TextView
            android:id="@+id/item_school_location"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:layout_marginStart="72dp"
            android:maxLines="1"
            android:textAppearance="@style/TextAppearance.AppCompat.Small"
            android:textSize="16sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toBottomOf="@id/item_school_name" />
    </android.support.constraint.ConstraintLayout>
</layout>

有人知道是什么导致了这个错误吗?

【问题讨论】:

    标签: android android-layout android-constraintlayout


    【解决方案1】:
    app:layout_constraintLeft_toLeftOf="parent"
    

    也许我不是完全最新的,但我认为该值不应该是“父” - 它应该是一个视图 ID(格式为“@+id/some_id”)。

    给你的约束布局一个 id,然后尝试使用它而不是 parent。

    编辑:我想我已经过时了?因为我刚刚又读了一遍,你的错误看起来像这行:

    layout_constraintBottom_toTopOf="@id/item_school_location"
    

    在 @ 之后缺少 + - 应该是这样

    layout_constraintBottom_toTopOf="@+id/item_school_location"
    

    【讨论】:

    • 哈!我有一个类似的问题。我没想到这会奏效,但确实如此。我想对 ID 的第一个引用(从顶部开始)应该有 + 号。
    • @amram99 - 据我所知,布局文件中对 id 的所有引用都应该在前面加上 +。试试看,我敢肯定,即使您的引用是 id 的第二次出现,如果没有 +,它仍然无法正常工作
    • 实际上 + 表示您正在分配一个新 ID。通常你会引用一个没有+的ID(例如在RelativeLayout中,但Android允许多次分配相同的ID。查看这个答案-stackoverflow.com/a/5731533/389643。但是由于Android使用元素的顺序来定义z-顺序,有时您必须在元素定义之上引用一个 ID,有时这会导致编译器问题。
    【解决方案2】:

    我觉得

    找不到与给定名称匹配的资源

    错误是由于您引用了在引用它的元素 (item_school_name) 之后声明的元素 (item_school_location) 的 id。

    在这种情况下,如果您在单独的 ids.xml 文件中声明相关元素的 id 会有所帮助(检查:https://stackoverflow.com/a/12247971

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-04
      • 2018-12-12
      • 2022-11-02
      • 2020-07-21
      相关资源
      最近更新 更多