我正在使用MVVM体系结构创建新应用。因此它与dataBinding有很强的联系。我想在MainActivity中使用DrawerLayout和数据绑定,但在这里我一直坚持,因为xml中DrawerLayout根元素的规则必须是DrawerLayout,为了成功进行数据绑定,根元素必须是layout。有人可以帮助我以正确的方式将这两个根本要素结合在一起吗?
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/actionBar_layout"
app:navGraph="@navigation/navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
</android.support.v4.widget.DrawerLayout>
</layout>
编译器告诉我DrawerLayout不能放置在布局标记中。如果我将布局放入DrawerLayout内,则数据绑定不再起作用
您应该将抽屉布局放在布局标记中,它可以正常工作。您的问题是您正在从android.support
获取抽屉布局,但您的约束布局是从androidx
获得的,我的建议是迁移到androidx