在添加抽屉布局时,必须使用 MeasureSpec.EXACTLY 来测量获取 DrawerLayout

问题描述

我正在尝试在我的片段主页中添加抽屉布局。我收到 java.lang.IllegalArgumentException: DrawerLayout 必须用 MeasureSpec.EXACTLY 测量。当我尝试将抽屉布局附加到我的片段时出现此错误

我有根布局作为约束布局。首先,我尝试使用根布局作为抽屉布局,但仍然没有收到此异常,因此我尝试向其添加约束布局但没有奏效。

当我将高度和宽度的硬编码值设置为 500dp 时,它会起作用。 但是不能给出硬编码值,因为对于所有设备大小来说这不是一个好习惯,它会有所不同。

下面是我的代码

抽屉布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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">

<androidx.drawerlayout.widget.DrawerLayout

    android:id="@+id/drawer"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/fragment_home"
        android:visibility="visible" />

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:layout_gravity="start">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <include
                android:id="@+id/nav_header"
                layout="@layout/nav_header" />

            <ScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fadeScrollbars="true"
                android:scrollbars="none">

                <include
                    android:id="@+id/menuItems"
                    layout="@layout/layout_navigation_menu" />
            </ScrollView>
        </LinearLayout>
    </com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

首页片段布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:fillViewport="true"
    android:fitsSystemWindows="true">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/root"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <include android:id="@+id/app_bar"
            layout="@layout/app_bar_main"/>

        <RelativeLayout
            android:id="@+id/relative_accept"
            android:layout_width="match_parent"
            android:layout_height="@dimen/margin_55"
            android:orientation="horizontal"
            android:background="@color/green_500"
            app:layout_constraintTop_toBottomOf="@id/app_bar">


            <androidx.appcompat.widget.AppCompatTextView
                android:layout_width="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_centerVertical="true"
                android:textColor="@color/white"
                android:layout_marginLeft="@dimen/margin_16"
                android:layout_marginStart="@dimen/margin_16"
                android:layout_marginRight="@dimen/margin_16"
                android:layout_marginEnd="@dimen/margin_16"
                android:text="@string/accepting_orders"
                android:fontFamily="@font/roboto_regular"
                android:textSize="@dimen/text_size_15"
                android:layout_alignParentLeft="true"
                android:layout_height="wrap_content" />

            <Switch
                android:id="@+id/switch1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:track="@drawable/switch_track_selector"
                android:layout_centerVertical="true"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                tools:ignore="UseSwitchCompatOrMaterialXml" />


        </RelativeLayout>

        <FrameLayout
            android:id="@+id/frameLayout_tab"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clipToPadding="false"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/relative_accept">

            <com.google.android.material.tabs.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="42dp"
                android:layout_marginStart="@dimen/margin_16"
                android:layout_marginLeft="@dimen/margin_16"
                android:layout_marginTop="@dimen/margin_20"
                android:layout_marginEnd="@dimen/margin_16"
                android:layout_marginRight="@dimen/margin_16"
                android:background="@drawable/grey_drawable"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:tabGravity="fill"
                app:tabIndicatorHeight="0dp"
                app:tabMode="fixed"
                android:onClick="@{(v)->click.onClick(v)}"
                app:tabBackground="@drawable/tab_selector"
                app:tabSelectedTextColor="@color/white" />
        </FrameLayout>

        <androidx.viewpager.widget.ViewPager
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintTop_toBottomOf="@+id/frameLayout_tab" />


    </androidx.constraintlayout.widget.ConstraintLayout>

</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

首页片段

class HomeFragment : BaseFragment(),View.OnClickListener{


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }
    override fun onCreateView(
        inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?
    ): View? = inflater.inflate(R.layout.drawer_layout_navigation_view,container,false)


    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        init()
        controlInit()
    }

    override fun init() {
        val toggle = ActionBarDrawerToggle(
            (activity as MainActivity),drawer,R.string.navigation_drawer_open,R.string.navigation_drawer_close
        )
        drawer.addDrawerListener(toggle)

        tabs.setupWithViewPager(view_pager)
        bindViewPagerAdapter(view_pager,(activity as MainActivity))
        bindViewPagerTabs(tabs,view_pager)
        tabSettings()
    }
    private fun tabSettings() {

        tabs.bringToFront()

        view_pager.addOnPagechangelistener(object : ViewPager.OnPagechangelistener {
            override fun onPageScrolled(
                    position: Int,positionOffset: Float,positionOffsetP: Int
            ) {
            }

            override fun onPageSelected(position: Int) {}
            override fun onPageScrollStateChanged(i: Int) {}
        })
    }

    private fun bindViewPagerAdapter(view: ViewPager?,mainActivity: MainActivity) {
        val adapter = OrdersAdapter(view?.context!!,childFragmentManager)
        view.adapter = adapter
    }

    private fun bindViewPagerTabs(view: TabLayout,pagerView: ViewPager?) {
        view.setupWithViewPager(pagerView,true)
    }

    private fun controlInit() {

        imageView_drawer.setonClickListener(this)

    }

    @SuppressLint("WrongConstant")
    override fun onClick(p0: View) {
        when (p0.id) {
            R.id.imageView_drawer -> {
                drawer.openDrawer(Gravity.START)
            }
        }
    }

    companion object {
        
        @JvmStatic
        fun newInstance() =
                HomeFragment().apply {}
    }
}

请帮忙解决这个问题。谢谢....

解决方法

当我测试时,您的布局实际上工作得很好,所以我不确定问题是否出在布局上。然而,您的布局文件过于复杂,您实际上并不需要任何现有的嵌套布局,无需也能获得相同的结果。 ConstraintLayout 旨在简化布局层次结构,因此您应该能够仅使用它来组织您的子视图。

此外,NavigationView 组件旨在用于显示菜单资源中的菜单,而不仅仅是用作您自己的包装器。要么选择完全显示您自己的菜单,要么按预期使用它们的实现。 [材料设计网站] here 上有一些很好的文档。

所以我建议您尝试减少嵌套布局,然后看看这是否能顺便解决您的问题。

您可能想要的层次结构可能如下所示:

drawer_layout.xml

<DrawerLayout>
    <include layout="@layout/home" />
    <NavigationView />
</DrawerLayout>

home.xml

<ScrollView>
    <ConstraintLayout>
        <include layout="@layout/app_bar_main"/>
        <TextView />
        <Switch />
        <TabLayout />
        <ViewPager />
    </ConstraintLayout>
</ScrollView>