《第一行代码》第十二章引用toolbar出错,从android升级到androidx

toolabr是Material控件(UI设计),由官方引入,使用Toolbar代替ActionBar所用。
在使用toolbar时,导入包时有所更新,查找后记录下,本文参考:as中出现错误:android.widget.Toolbar cannot be cast to androidx.appcompat.widget.Toolbar

本文在《第一行代码第二版》12.2基础上调试。
更改处:

  1. app/build.grade
    implementation ‘com.android.support:appcompat-v7:28.0.0’
  2. activity_main.xml
    <androidx.appcompat.widget.Toolbar
    …/>
  3. MainActivity.class
    import androidx.appcompat.widget.Toolbar;

猜测原因就是androidx的更改…

  • 12
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
如果你在使用 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 控件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值