如何使用 tools:context 关联 Fragment 文件?

Dha*_*val 4 android android-layout android-xml

最近我遇到了tools:context在我的布局文件中使用活动的用例。我了解到我可以使用它来将 Activity 类文件关联到我的布局文件。

但是我如何使用它来将 Fragment 类文件与任何布局相关联呢?

Bacuase 正如我所尝试的那样,它仅在自动完成建议中显示活动列表。

Arm*_*ght 5

Android Studio 有一个bug,你必须手动编写它,它会显示为红色(表示未解决),直到你输入完成,然后它才会显示为绿色


Uma*_*air 2

你有没有尝试过这样做:

假设Relative layout您的布局中是您的父布局fragment's

<RelativeLayout 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=".Folder.Fragment"  >
Run Code Online (Sandbox Code Playgroud)

.Fragment class现在这个 .xml 文件将与您的片段相关联。