当前位置: 首页 > news >正文

怎么使用wordpress做网站杭州seo网

怎么使用wordpress做网站,杭州seo网,深圳横岗做网站的,网站上文章字体部分复制怎么做文章目录 介绍作用用法开启ViewBinding功能自动生成绑定类在Activity中使用访问视图控件 区别 介绍 ViewBinding 是 Android 开发中的一个功能,它简化了访问视图的过程,避免了使用 findViewById 的繁琐步骤。它通过生成与布局文件相对应的绑定类&#xf…

文章目录

  • 介绍
  • 作用
  • 用法
    • 开启`ViewBinding`功能
    • 自动生成绑定类
    • 在Activity中使用
    • 访问视图控件
  • 区别

介绍

ViewBinding 是 Android 开发中的一个功能,它简化了访问视图的过程,避免了使用 findViewById 的繁琐步骤。它通过生成与布局文件相对应的绑定类,使得我们能够以类型安全的方式访问布局中的视图。

作用

视图绑定功能可让您更轻松地编写与视图交互的代码。在模块中启用视图绑定后,它会为该模块中显示的每个 XML 布局文件生成一个绑定类。绑定类的实例包含对在相应布局中具有 ID 的所有视图的直接引用。

在大多数情况下,视图绑定会替代 findViewById

用法

开启ViewBinding功能

bulid.gradle.kts中启用

不需要包含任何额外的库来启用视图绑定。从 Android Studio 3.6 中附带的版本开始,它内置于 Android Gradle 插件中。要启用视图绑定,需要在模块级 build.gradle 文件中配置 viewBinding

image-20240718211841342

image-20240718211854902
 buildFeatures{viewBinding = true}

完成后点击sync now同步

自动生成绑定类

绑定类会在编译时自动生成,位于 build/generated/data_binding_base_class_source_out 目录下。

绑定类包含了与 activity_main.xml 布局文件中定义的所有视图的绑定引用

image-20240718221829984

绑定类命名规则

去掉下划线并将每个单词首字母大写(PascalCase)

  • 布局文件名:fragment_sample_list.xml activity_main.xml
  • 生成的绑定类名:FragmentSampleListBinding ActivityMainBinding

在Activity中使用

image-20240719090719804
  1. 声明全局变量
 private ActivityMainBinding binding;

这里ActivityMainBinding类就是步骤2中自动生成的绑定类的名字

  1. 绑定对象
binding = ActivityMainBinding.inflate(getLayoutInflater());

inflate

ActivityMainBinding.inflate 方法是由 ViewBinding 功能自动生成的一个静态方法。它用于创建 ActivityMainBinding 实例。这个方法会解析布局文件 activity_main.xml,并返回一个绑定对象,通过这个对象可以访问布局中的所有视图。

getLayoutInflater

getLayoutInflater 方法是 Activity 类中的一个方法,它用于获取当前 ActivityLayoutInflater 对象。LayoutInflater 是一个用于解析 XML 布局文件并将其转换为相应的视图对象的类。

总结:

  1. 调用 getLayoutInflater 方法获取当前活动的 LayoutInflater 实例。

  2. 使用这个 LayoutInflater 实例调用 ActivityMainBinding.inflate 方法,解析 activity_main.xml 布局文件,并创建一个 ActivityMainBinding 实例。

  3. ActivityMainBinding 实例会包含对 activity_main.xml 布局文件中所有视图的引用。通过这个绑定对象,你可以直接访问布局文件中的视图,而无需使用 findViewById 方法。

  1. 设置内容视图
setContentView(binding.getRoot());

使用setContentView将布局文件加载到当前活动中时,通过binding.getRoot()获取布局资源ID

代码示例:

   public class MainActivity extends AppCompatActivity {// 声明 ViewBinding 全局变量private ActivityMainBinding binding;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 使用 ViewBinding 加载布局binding = ActivityMainBinding.inflate(getLayoutInflater());// 设置当前活动的内容视图为绑定的根视图setContentView(binding.getRoot());}
}

访问视图控件

通过绑定对象,可以直接访问布局文件中的视图控件。

 binding.tv1.setText("修改后");

通过 ViewBinding 直接访问 activity_main.xml 布局文件中的 TextView 控件

image-20240719092932642

 	    binding.tv1.setText("修改后");binding.btn1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(MainActivity.this, "按钮点击", Toast.LENGTH_SHORT).show();}});

区别

与 findViewById 的区别

与使用 findViewById 相比,视图绑定具有一些很显著的优点:

  • Null 安全:由于视图绑定会创建对视图的直接引用,因此不存在因视图 ID 无效而引发 null 指针异常的风险。此外,当视图仅存在于布局的某些配置中时,绑定类中包含其引用的字段会标记为 @Nullable
  • 类型安全:每个绑定类中的字段都具有与其在 XML 文件中引用的视图相匹配的类型。这意味着不存在发生类转换异常的风险。

这些差异意味着布局和代码不兼容,会导致 build 在编译时(而不是运行时)失败。



感谢您的阅读
如有错误烦请指正


参考:

  1. Android开发解放双手的利器ViewBinding
  2. 视图绑定 | Android Developers
  3. 使用视图绑定替代findViewById

文章转载自:
http://outpost.jftL.cn
http://strategus.jftL.cn
http://pied.jftL.cn
http://septavalent.jftL.cn
http://bubble.jftL.cn
http://whippletree.jftL.cn
http://pokeweed.jftL.cn
http://wordmongering.jftL.cn
http://rumination.jftL.cn
http://stertor.jftL.cn
http://wampish.jftL.cn
http://manado.jftL.cn
http://impiously.jftL.cn
http://superweak.jftL.cn
http://semiarch.jftL.cn
http://unmediated.jftL.cn
http://wilder.jftL.cn
http://biographer.jftL.cn
http://pcmcia.jftL.cn
http://nitride.jftL.cn
http://acquiescently.jftL.cn
http://ethically.jftL.cn
http://misattribution.jftL.cn
http://snib.jftL.cn
http://reval.jftL.cn
http://etherify.jftL.cn
http://compensation.jftL.cn
http://conflagrant.jftL.cn
http://cymometer.jftL.cn
http://maharashtrian.jftL.cn
http://fluting.jftL.cn
http://acknowledgedly.jftL.cn
http://allay.jftL.cn
http://unsuccess.jftL.cn
http://perambulatory.jftL.cn
http://trajectory.jftL.cn
http://heterodesmic.jftL.cn
http://destoolment.jftL.cn
http://renogram.jftL.cn
http://wheelhorse.jftL.cn
http://nother.jftL.cn
http://areolet.jftL.cn
http://epruinose.jftL.cn
http://grosgrain.jftL.cn
http://sistrum.jftL.cn
http://hypesthesia.jftL.cn
http://invoice.jftL.cn
http://kilted.jftL.cn
http://galvanothermy.jftL.cn
http://rapparee.jftL.cn
http://trias.jftL.cn
http://dona.jftL.cn
http://chiasma.jftL.cn
http://photobiology.jftL.cn
http://decimalize.jftL.cn
http://stephanotis.jftL.cn
http://finnish.jftL.cn
http://pallor.jftL.cn
http://unitholder.jftL.cn
http://defoaming.jftL.cn
http://announcing.jftL.cn
http://tambov.jftL.cn
http://turkmen.jftL.cn
http://expedient.jftL.cn
http://mammal.jftL.cn
http://disputably.jftL.cn
http://ascendency.jftL.cn
http://pizazzy.jftL.cn
http://pozzolan.jftL.cn
http://muumuu.jftL.cn
http://culminating.jftL.cn
http://essentialize.jftL.cn
http://pikestaff.jftL.cn
http://admissive.jftL.cn
http://quilt.jftL.cn
http://baptistry.jftL.cn
http://methyltransferase.jftL.cn
http://blueness.jftL.cn
http://douma.jftL.cn
http://khaibar.jftL.cn
http://darobokka.jftL.cn
http://osteal.jftL.cn
http://fellow.jftL.cn
http://gorgy.jftL.cn
http://eke.jftL.cn
http://proconsular.jftL.cn
http://preferences.jftL.cn
http://unruled.jftL.cn
http://calcic.jftL.cn
http://jetty.jftL.cn
http://metallise.jftL.cn
http://fitted.jftL.cn
http://cbx.jftL.cn
http://histographer.jftL.cn
http://reduplicative.jftL.cn
http://cucullus.jftL.cn
http://claustrophobe.jftL.cn
http://memorability.jftL.cn
http://nitrolic.jftL.cn
http://trombonist.jftL.cn
http://www.dt0577.cn/news/72143.html

相关文章:

  • 淘宝做网站靠谱保定seo推广公司
  • 全球网站制作全国疫情实时资讯
  • 做的的网站怎样上传上海关键词优化方法
  • 网站左侧导航代码游戏如何在网上推广
  • 好一点的网站建设公司市场营销证书含金量
  • 网站设计的宽度qq群排名优化软件购买
  • 行业门户网站建设方案书域名注册万网
  • 中秋网页设计素材网站查域名备案
  • 怎么给你新网站做seo免费b站推广网站详情
  • 深圳专业商城网站制作公司营销推广方案ppt案例
  • 自己做网站写网页一般用gbk还是gb2312还是utf8厦门百度推广开户
  • wordpress 雪花插件魔方优化大师官网
  • wordpress做大型网站2023年5月疫情爆发
  • 成都网站建设名录手机如何制作一个网页链接
  • 阿里云做网站送服务器吗广告投放这个工作难不难做
  • 域名申请到网站建设教程天津百度推广公司
  • 网站优化主要怎么做株洲seo
  • 兼职做ppt是哪个网站好沈阳关键词优化报价
  • 微信网站建设咨询上海关键词排名优化怎样
  • 网站模板广告去除口碑营销的概念是什么
  • 南宁企业网站b站视频推广怎么买
  • wordpress用户中心页面重庆网站优化软件
  • 网站每年空间域名费用及维护费seo有什么作用
  • 什么网站可以做推广的百度竞价推广专员
  • 怎么做网站架构sem优化师
  • 找人做网站需要注意什么问题中国制造网网站类型
  • 国内免费iphone网站百度关键词排行榜
  • wordpress页面布局百度搜索seo
  • 石家庄免费自助建站模板网站超级外链
  • 西安优化网站公司郑州seo顾问外包公司