Android之viewBing(视图绑定)

2023-05-16

首先我们需要从Android studio模块编译脚本中加入依赖:

    viewBinding {
        enabled = true

    }
  • 如图
    在这里插入图片描述

布局文件

在这里插入图片描述

放置了三个button

 <Button
     android:layout_below="@+id/id_text01"
     android:text="@string/view_binding"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/id_button_1"
     ></Button>
 <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/id_button_2"
     android:layout_below="@id/id_button_1"
     />

 <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/id_button_3"
     android:layout_below="@id/id_button_2"
     android:text="@string/there_"
     />

加载布局

  • 以前加载布局
// 老办法
setContentView(R.layout.activity_main);
  • 现在加载布局
 // 新办法
 ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater());
 setContentView(binding.getRoot());
  • 对比
 // 老办法
 setContentView(R.layout.activity_main);
 // 新办法
 ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater());
 setContentView(binding.getRoot());

获得控件

  • 以前

findViewById(R.id.id_button_3).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(MainActivity.this, "我是传统发放除法的", Toast.LENGTH_SHORT).show();
    }
});
  • 现在
binding.idButton2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(MainActivity.this,"2222", Toast.LENGTH_SHORT).show();
    }
});
  • 对比
binding.idButton2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(MainActivity.this,"2222", Toast.LENGTH_SHORT).show();
    }
});

findViewById(R.id.id_button_3).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(MainActivity.this, "我是传统发放除法的", Toast.LENGTH_SHORT).show();
    }
});

需要说明的

首先可以注意到在xml的布局文件里面,控件id的名命都是依靠下划线来进行分割的。
在这里插入图片描述
但是在java代码中调用的时候,却丢失了下划线转化成了驼峰命名。
在这里插入图片描述
这是因为Android view的优化机制导致的。
如果想要细看的话可以打开自己编译生成的文件。会多出来一个文件就是viewbinding自动生成的文件。
在这里插入图片描述

// Generated by view binder compiler. Do not edit!
package com.example.learnandroidfromgoogle.databinding;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.viewbinding.ViewBinding;
import com.example.learnandroidfromgoogle.R;
import java.lang.NullPointerException;
import java.lang.Override;
import java.lang.String;

public final class ActivityMainBinding implements ViewBinding {
  @NonNull
  private final ConstraintLayout rootView;

  @NonNull
  public final Button idButton1;

  @NonNull
  public final Button idButton2;

  @NonNull
  public final RelativeLayout idRelative;

  @NonNull
  public final TextView idText01;

  private ActivityMainBinding(@NonNull ConstraintLayout rootView, @NonNull Button idButton1,
      @NonNull Button idButton2, @NonNull RelativeLayout idRelative, @NonNull TextView idText01) {
    this.rootView = rootView;
    this.idButton1 = idButton1;
    this.idButton2 = idButton2;
    this.idRelative = idRelative;
    this.idText01 = idText01;
  }

  @Override
  @NonNull
  public ConstraintLayout getRoot() {
    return rootView;
  }

  @NonNull
  public static ActivityMainBinding inflate(@NonNull LayoutInflater inflater) {
    return inflate(inflater, null, false);
  }

  @NonNull
  public static ActivityMainBinding inflate(@NonNull LayoutInflater inflater,
      @Nullable ViewGroup parent, boolean attachToParent) {
    View root = inflater.inflate(R.layout.activity_main, parent, false);
    if (attachToParent) {
      parent.addView(root);
    }
    return bind(root);
  }

  @NonNull
  public static ActivityMainBinding bind(@NonNull View rootView) {
    // The body of this method is generated in a way you would not otherwise write.
    // This is done to optimize the compiled bytecode for size and performance.
    String missingId;
    missingId: {
      Button idButton1 = rootView.findViewById(R.id.id_button_1);
      if (idButton1 == null) {
        missingId = "idButton1";
        break missingId;
      }
      Button idButton2 = rootView.findViewById(R.id.id_button_2);
      if (idButton2 == null) {
        missingId = "idButton2";
        break missingId;
      }
      RelativeLayout idRelative = rootView.findViewById(R.id.id_relative);
      if (idRelative == null) {
        missingId = "idRelative";
        break missingId;
      }
      TextView idText01 = rootView.findViewById(R.id.id_text01);
      if (idText01 == null) {
        missingId = "idText01";
        break missingId;
      }
      return new ActivityMainBinding((ConstraintLayout) rootView, idButton1, idButton2, idRelative,
          idText01);
    }
    throw new NullPointerException("Missing required view with ID: ".concat(missingId));
  }
}

本文参考了以下内容:

  • Android官网教程
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android之viewBing(视图绑定) 的相关文章

随机推荐

  • 闲着看看jquery.ajax源码

    框架的作用就是简化我们做的事情 xff0c 却又不失灵活性 jquery是js框架中的中流砥柱 xff0c 灵活并且强大 jquery中对ajax的封装很完美 xff0c 且不说底层的ajax函数的强大 xff0c 但是其上层的get xf
  • 关于jquery对象的remove参数中出现伪位置类选择器,出现非预期结果的研究

    记得前几天有人在论坛发帖问了一个关于jquery删除节点的问题 原帖是这样的 xff08 原帖的地址是 xff1a 原帖 xff09 lt ul gt lt li gt 1 lt li gt lt li title 61 34 a 34 g
  • linux源码阅读利器-GNU GLOBAL Source Code Tag System

    学习浏览linux源码 xff0c 如果自己用自带的文本编辑器就太麻烦了 xff0c 但是如果安装强大的lxr那就太麻烦了 xff08 对于非debian用户来说 xff09 xff0c 找了很久找到了一个用起来很不错的源码浏览工具 xff
  • android 4.0.1源码编译,学习错误解决

    主机是fedora 14 linux内核2 6 35 6 swapon交换分区1 5G make version 3 81 官方指定的make版本 xff09 jdk 1 6 磁盘预留空间大概需要大于13G 具体的编译步骤可参考 Fedor
  • android webApp 调试问题解决

    前不久做了个webapp xff0c 在pc上chrome调试都是可以的 但是手机上显示却有点问题 xff0c 所以一直是想在手机浏览器上调试 xff0c 但是一直没有相关支持 xff0c 后来google终于出了chrome beta版
  • android系统源代码分析 书评

    其实接触android应用开发差不多两年了 xff0c 但是实际上并没与多少拿得出手的作品 因为在很长一段时间里我都在问自己android是什么 xff0c 内部怎么运行的 xff0c 为什么我的java代码就可以在linux上运行 xff
  • Fedora18博通430g电信无线拨号上网

    回家折腾了有两三天 xff0c 想让fedora18 也能蹭上网 首当其冲的问题就是fedora 没有为无线网卡安装驱动 xff0c 只能自己慢慢找驱动 费了好久找到上传了驱动下载 xff0c 满心欢喜按照README 编译 xff0c 结
  • VR技术的发展趋势,未来有哪些展望?

    虚拟现实技术Virtual Reality xff0c 缩写为VR xff0c 是一项全新的实用技术 虚拟现实技术包含计算机 电子信息 仿真技术于一体 xff0c 其基本实现方式是计算机模拟虚拟环境从而给人以环境沉浸感 目前来看 xff0c
  • 1.1 操作系统的基本概念

    一 操作系统的基本概念 span class token number 1 span 计算机系统自上而下可大致分为 span class token number 4 span 部分 span class token punctuation
  • 线性代数之——特征值和特征向量

    线性方程 A x 61 b Ax 61 b A x 61 b 是稳定状态的问题 xff0c 特征值在动态问题中有着巨大的重要性
  • PowerShell 学习笔记:压缩、解压缩文件

    在自动构建的时候 xff0c 最常用的就是压缩备份项目的源文件 xff0c PowerShell提供了相关命令 Compress Archive xff08 压缩文件 xff09 Compress Archive Path lt Strin
  • MySQL和MariaDB,它们有什么区别?

    目录 一 MySQL简介 二 MariaDB简介 三 什么是MariaDB 四 为什么推出MariaDB xff1f 五 主要区别 六 总结 在这篇文章中 xff0c 我们将探讨MySQL和MariaDB之间的区别 两者都是开源的关系型数据
  • 【没有anaconda powershell prompt】Anaconda安装后开始菜单没有Anaconda Powershell Prompt

    Anaconda安装后开始菜单没有Anaconda Powershell Prompt 昨天想做点东西 xff0c 发现没有anaconda powershell prompt xff0c 关键还需要用到他 xff0c 网上找了好久都没有关
  • Little Boxes (C++大数加法)

    ii 9ABH SA391 r9GG35 amp G amp ii13Gh i3X31i rB1 iMs i5895 5G91 s1 8A
  • 基于 Github/Gitee Pages 服务+ Hexo框架搭建静态博客

    基于 Github Gitee Pages 服务 43 Hexo框架搭建静态博客 文章目录 基于 96 Github 96 96 Gitee Pages 96 服务 43 96 Hexo 96 框架搭建静态博客前言 xff1a 要知道些什么
  • JVM内存分配机制

    Java虚拟机最重要的工作就是如何给对象分配内存空间 xff0c 以及通过GC如何回收已经不再使用的内存空间 这篇文章主要介绍JVM中的Java对象是创建过程 对象内存的分配机制以及对象内存的回收机制 一 对象的创建 在前面的文章 JVM类
  • Android开发——View Binding的使用与解析

    1 前言 如何干掉模版代码是很多第三方框架的设计初衷 xff0c 在Android开发中 xff0c findViewById 是必不可少的存在 xff0c 这样的冗余代码在很久以前充斥在Android工程中 xff0c 因此也出现了很多精
  • linux网络编程的一些基础知识--基础API相关

    大多数套接字函数都需要一个指向套接字地址结构的指针作为参数 IPV4套接字地址结构 struct in addr in addr t s addr 32bits struct sockaddr in uint8 t sin len 8bit
  • AndroidStudio - 无法运行应用的解决方法

    有时桑Adb端口被占用时会出现无法运行应用并出现 Installation did not succeed The application could not be installed 的问题 最新补充解决方案 xff1a 可直接杀死adb
  • Android之viewBing(视图绑定)

    首先我们需要从Android studio模块编译脚本中加入依赖 xff1a viewBinding enabled 61 true 如图 布局文件 放置了三个button span class token tag span class t