首页 > 解决方案 > ANDROID - “ID“示例”已定义但未分配给任何视图”

问题描述

我在 ids.xml 中定义了 ID -><item name="example" type="id"/>

我在styles.xml中使用了这个ID,就像这样->

<style name="exampleStyle">
     <item name="android:id">@id/example</item>
     ###rest of items (width, height etc.)
</style>

我在我的 ConstraintLayout 中使用了这种风格 ->

    <Button
        android:id="@+id/testButton"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toStartOf="@+id/goNextButton"/>

    <ImageView
        style="@style/exampleStyle"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

最后 IDE(Android 工作室)显示错误“id 示例已定义但未分配给任何视图。

为什么它没有看到我只是通过样式使用了这个 id?我可以在不使用的情况下解决这个问题tools:ignore="UnknownId吗?

标签: androidxmlandroid-layoutandroid-styles

解决方案


android:id而不是在样式中使用“ ”,而是在ImageView

例如,在一个布局中,它可能有几个ImageView. 所以你可以多次使用相同的样式。但是如果动作是基于 id 的,那么你就不能正确地使用它。


推荐阅读