如何使用 kotlin 从 adroid studio 的 res 文件夹中导入文件?

问题描述 投票:0回答:1

我在名为

activiy_main.xml
的 xml 中创建了一个 recyclerView 变量,我想在 MainActivity 文件中访问它 我尝试了不同的方法来导入 xml 文件,但每次都会出错。 我在项目中添加了导航图片,我缺少什么?

enter image description here

android kotlin android-studio import importerror
1个回答
0
投票

在您的

activiy_main.xml
中,您的回收者视图应该看起来像这样

<androidx.recyclerview.widget.RecyclerView
                            android:id="@+id/my_recyclerview"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="horizontal"
                            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
                            tools:listitem="@layout/featured_product_item"/>

在您的活动中,您可以像这样访问它:

val recyclerview = findViewById<RecyclerView>(R.id.my_recyclerview)
© www.soinside.com 2019 - 2024. All rights reserved.