首页 > 解决方案 > 在 Android Studio 中,XML 布局中视图 ID 的“查找用法”操作不适用于 Android 视图绑定

问题描述

Android 中的View Binding被大力提倡作为一个更好的替代findViewById()甚至 Kotlin 扩展,但出于某种原因,在 Android Studio 的布局编辑器中通过 ID 搜索 a 的使用的最基本功能View不起作用。

它甚至在官方 Acrh 组件示例存储库中的视图绑定示例中也不起作用。
在这里,我们有:

  1. fragment_blank.xml带有TextViewID的XML 布局textViewFragment如下:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".InflateFragment">

    <TextView
        android:id="@+id/textViewFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>
  1. 使用视图绑定InflateFragment.kt将文本设置为此的片段:TextView
override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val binding = FragmentBlankBinding.inflate(inflater, container, false)
        fragmentBlankBinding = binding
        binding.textViewFragment.text = getString(R.string.hello_from_vb_inflatefragment)
        return binding.root
    }
  1. 因此,当我在布局编辑器中选择此 ID 并从上下文菜单中执行“查找用法”操作时: 在此处输入图像描述

  2. 实际上没有找到任何东西,但我们清楚地看到我们正在为这个文本视图设置文本InflateFragment.kt 在此处输入图像描述

关于如何通过View布局编辑器中的 ID 搜索此类用法的任何想法?
无法相信视图绑定缺少这样的基本功能。

安卓工作室版本4.0.1
摇篮版本6.6.1
Android Gradle 插件版本4.0.1

标签: androidandroid-studioandroid-viewbinding

解决方案


推荐阅读