【安卓开发】DrawerLayout+ToolBar报错:DrawerLayout must be measured with MeasureSpec.EXACTLY.

一、说明

在使用ToolBar+DrawerLayout的时候,总是报如下的错误:

DrawerLayout must be measured with MeasureSpec.EXACTLY.

搜索了那么多答案意思就是DrawerLayout的宽高不能是设置成match_parent或者wrap_content,只能设置成具体的数值,比如320dp之类的。试了一下,确实是可以,但是:

  1. 其他人设置成match_parent为什么都能正常?
  2. 直接设置成具体数值,很难适配所有尺寸的设备,显然行不通。

二、问题所在

后来在看第N遍代码时终于发现了问题,就是在设置Drawerlayout的第二个view时,本应该设置一个layout_gravity属性,但是我设置成了gravity属性,正确的应该是下面这样:

android:layout_gravity="start"

layout_gravitygravity的区别是前者设置view本身相对父视图的位置,后者设置view视图里面控件的位置的。但不知道为什么这边会引起报这个错。其他人的情况不知道是怎样,我的是如此,希望对大家有用。

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你在使用 ConstraintLayout 布局时,遇到了 "Missing Constraints in ConstraintLayout" 的错误,这意味着你的 Toolbar 控件缺少了必要的约束条件。 在 ConstraintLayout 中,每个控件都需要定义水平和垂直方向上的约束条件,以确定其在布局中的位置。对于 Toolbar,你可以尝试添加以下约束条件: 1. 左侧约束条件:将 Toolbar 的左侧边缘与父布局的左侧边缘连接。 ``` app:layout_constraintStart_toStartOf="parent" ``` 2. 右侧约束条件:将 Toolbar 的右侧边缘与父布局的右侧边缘连接。 ``` app:layout_constraintEnd_toEndOf="parent" ``` 3. 顶部约束条件:将 Toolbar 的顶部边缘与父布局的顶部边缘连接。 ``` app:layout_constraintTop_toTopOf="parent" ``` 这些约束条件将确保 Toolbar 控件在水平和垂直方向上都有合适的位置。记得将这些约束条件添加到 Toolbar 控件的 XML 声明中,例如: ```xml <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:elevation="4dp" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" /> ``` 通过添加这些约束条件,你应该能够解决 "Missing Constraints in ConstraintLayout" 的错误,并正确地布局 Toolbar 控件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值