如何在 ViewBinding 中使用片段?

ch6*_*h65 6 android fragment android-viewbinding

我想用 ViewBinding 创建一个片段,但我的代码不起作用。我阅读了ViewBinding 文档,但我的片段没有显示。这是我的代码:

片段播放器.xml

<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Test fragment"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

布局_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment
    android:id="@+id/fragment_player"
    class="com.test.plo.view.PlayerFragment"
    android:name="com.test.plo.view.PlayerFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

PlayerFragment.java

 public class PlayerFragment extends Fragment {

   private FragmentPlayerBinding fragmentPlayerBinding;

   @Nullable
   @Override
   public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

       fragmentPlayerBinding = FragmentPlayerBinding.inflate(inflater, container, false);
       View view = fragmentPlayerBinding.getRoot();
       return view;
   }

  @Override
  public void onDestroyView() {
      super.onDestroyView();
      fragmentPlayerBinding = null;
  }

}
Run Code Online (Sandbox Code Playgroud)

谁能帮我?

and*_*aso 0

你是否尝试过:

android {
    ...
    buildFeatures {
    viewBinding true
    }
}
Run Code Online (Sandbox Code Playgroud)

在你的应用程序 build.gradle 中?

  • 我建议不要在回答中使用反问句。他们可能会被误解为根本不是答案。您正在尝试回答本页顶部的问题,不是吗?否则请删除此帖子。 (3认同)