Android架构组件--ViewBinding学习笔记

Android架构组件–ViewBinding学习笔记1.解决的问题ViewBinding的目的是为了减少样板代码findViewById(int)的使用,提升开发效率,精简Activity和Fragment的代码。2.使用ViewBindingViewBinding的使用需要Android Studio版本为3.6以上,需要在模块的build.gradle文件中手动开启,如下:android { ... buildFeatures { viewBinding =
摘要由CSDN通过智能技术生成

Android架构组件–ViewBinding学习笔记

1.解决的问题

ViewBinding的目的是为了减少样板代码findViewById(int)的使用,提升开发效率,精简ActivityFragment的代码。

2.使用ViewBinding

ViewBinding的使用需要Android Studio版本为3.6以上,需要在模块的build.gradle文件中手动开启,如下:

android {
    ...

    buildFeatures {
        viewBinding = true
    }
}

之后Android Studio会自动为layout文件夹下的布局文件生成对应的Java类,命名规则为文件名的大驼峰命名并在后面加上Binding

比如activity_main.xml布局文件如下:

<?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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/timestampText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="@color/colorPrimary"
        android:gravity="center"
        android:padding="20dp"
        tools:text="Hello world"
        app:layout_constraintTop_toTopOf="parent"/>

    <Button
        android:id="@+id/timestampButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraint
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值