栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

studio3,致Android开发者

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

studio3,致Android开发者

    viewBinding {

      enabled =

      true

        }

          }

          3.3、Activity中ViewBinding的使用

            //之前设置视图的方法

              setContentView(R.layout.activity_main);

                //使用ViewBinding后的方法

                  mBinding = ActivityMainBinding.inflate(getLayoutInflater());

                    setContentView(mBinding.getRoot());

                    可以看到,当你使用了ViewBinding后,针对你的activity_main.xml文件,会自动帮你生成一个ActivityMainBinding.java文件(该文件在build/generated/data_binding_base_class_source_out/xxx…目录下),也就是布局文件的驼峰命名法加上一个Binding后缀,然后在Activity中直接使用就可以。

                    3.3.1、布局中直接的控件

                    当我们在布局中添加一个id为 tv_text 的TextView后,直接在Activity中使用mBinding.tvText即可拿到该控件。如下所示,可以看到也是以控件ID的驼峰命名法来获取的:

                    mBinding.tvText.setText("是你得不到的ViewBinding");

                    3.3.2、布局中使用include

                    例如我们有个layout_comment.xml的布局,布局中有id为tv_include的TextView,代码如下:

                        “http://schemas.android.com/apk/res/android”

                          android:layout_width=

                          “match_parent”

                            android:layout_height=

                            “match_parent”>

                                android:id=

                                “@+id/tv_include”

                                  android:text=

                                  “这就是测试啊”

                                    android:layout_width=

                                    “match_parent”

                                      android:layout_height=

                                      “match_parent” />

                                        然后在activity_main.xml文件中include该布局:

                                            “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”>

                                                          android:id=

                                                          “@+id/tv_text”

                                                            android:layout_width=

                                                            “wrap_content”

                                                              android:layout_height=

                                                              “wrap_content”

                                                                android:text=

                                                                “Hello World!”

                                                                  app:layout_constraintBottom_toBottomOf=

                                                                  “parent”

                                                                    app:layout_constraintLeft_toLeftOf=

                                                                    “parent”

                                                                      app:layout_constraintRight_toRightOf=

                                                                      “parent”

                                                                        app:layout_constraintTop_toTopOf=

                                                                        “parent” />

                                                                            android:id=

                                                                            “@+id/layout_include”

                                                                              layout=

                                                                              “@layout/layout_comment” />

                                                                                那么此时我们如何使用到layout_comment.xml布局中的TextView控件呢,首先include标签需要声明id,例如layout_include,然后Activity中代码如下:

                                                                                mBinding.layoutInclude.tvInclude.setText("这就是你的不对了");

                                                                                是不是很神奇,是不是很简单。

                                                                                注意:

                                                                                当你给layout_comment.xml的根布局再添加id(比如添加了layout_xxx的ID)的时候,此时会报错:

                                                                                java.lang.NullPointerException: Missing required view with ID: layout_xxx

                                                                                3.3.2、布局中使用include和merge

                                                                                我们将上文的layout_comment.xml稍作修改,根布局使用merge标签,其他不做修改:

                                                                                    “http://schemas.android.com/apk/res/android”

                                                                                      android:layout_width=

                                                                                      “match_parent”

                                                                                        android:layout_height=

                                                                                        “wrap_content”>

                                                                                            android:id=

                                                                                            “@+id/tv_include”

                                                                                              android:text=

                                                                                              “这就是测试啊”

                                                                                                android:gravity=

                                                                                                “end”

                                                                                                  android:layout_width=

                                                                                                  “match_parent”

                                                                                                    android:layout_height=

                                                                                                    “match_parent” />

                                                                                                          “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”>

                                                                                                                        android:id=

                                                                                                                        “@+id/layout_include”

                                                                                                                          layout=

                                                                                                                          “@layout/layout_comment” />

                                                                                                                            activity_main.xml文件中使用include添加该布局后,在java代码中依旧是可以正常使用以下代码的:

                                                                                                                            mBinding.layoutInclude.tvInclude.setText("会不会出现问题呀");

                                                                                                                            但是但是!!!运行就会报错:

                                                                                                                            java.lang.NullPointerException: Missing required view with ID: layoutInclude

                                                                                                                            要是把include标签的id去掉的话,这时mBinding中也是找不到tvInclude这个控件呀,怎么办??

                                                                                                                            之前是不是说过,每个layout文件都会对应一个Binding文件,那么layout_comment.xml,肯定也有一个LayoutCommentBinding.java文件,我们去看下这个文件的源代码,里面有个可疑的方法,bind()方法:

                                                                                                                              @NonNull

                                                                                                                                public static LayoutCommentBinding 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: {

                                                                                                                                          TextView tvInclude = rootView.findViewById(R.id.tv_include);

                                                                                                                                            if (tvInclude ==

                                                                                                                                            null) {

                                                                                                                                              missingId =

                                                                                                                                              “tvInclude”;

                                                                                                                                                break missingId;

                                                                                                                                                  }

                                                                                                                                                    return

                                                                                                                                                    new LayoutCommentBinding(rootView, tvInclude);

                                                                                                                                                      }

                                                                                                                                                        throw

                                                                                                                                                        new NullPointerException(

                                                                                                                                                        "Missing required view with ID: ".concat(missingId));

                                                                                                                                                          }

                                                                                                                                                          学习交流

                                                                                                                                                          如果你觉得自己学习效率低,缺乏正确的指导,可以加入资源丰富,学习氛围浓厚的技术圈一起学习交流吧!

                                                                                                                                                          群内有许多来自一线的技术大牛,也有在小厂或外包公司奋斗的码农,我们致力打造一个平等,高质量的Android交流圈子,不一定能短期就让每个人的技术突飞猛进,但从长远来说,眼光,格局,长远发展的方向才是最重要的。

                                                                                                                                                          35岁中年危机大多是因为被短期的利益牵着走,过早压榨掉了价值,如果能一开始就树立一个正确的长远的职业规划。35岁后的你只会比周围的人更值钱。

                                                                                                                                                          ding中也是找不到tvInclude这个控件呀,怎么办??

                                                                                                                                                          之前是不是说过,每个layout文件都会对应一个Binding文件,那么layout_comment.xml,肯定也有一个LayoutCommentBinding.java文件,我们去看下这个文件的源代码,里面有个可疑的方法,bind()方法:

                                                                                                                                                            @NonNull

                                                                                                                                                              public static LayoutCommentBinding 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: {

                                                                                                                                                                        TextView tvInclude = rootView.findViewById(R.id.tv_include);

                                                                                                                                                                          if (tvInclude ==

                                                                                                                                                                          null) {

                                                                                                                                                                            missingId =

                                                                                                                                                                            “tvInclude”;

                                                                                                                                                                              break missingId;

                                                                                                                                                                                }

                                                                                                                                                                                  return

                                                                                                                                                                                  new LayoutCommentBinding(rootView, tvInclude);

                                                                                                                                                                                    }

                                                                                                                                                                                      throw

                                                                                                                                                                                      new NullPointerException(

                                                                                                                                                                                      "Missing required view with ID: ".concat(missingId));

                                                                                                                                                                                        }

                                                                                                                                                                                        学习交流

                                                                                                                                                                                        如果你觉得自己学习效率低,缺乏正确的指导,可以加入资源丰富,学习氛围浓厚的技术圈一起学习交流吧!

                                                                                                                                                                                        [外链图片转存中…(img-0SU1UqK6-1646480286092)]

                                                                                                                                                                                        [外链图片转存中…(img-eodCfbeW-1646480286093)]

                                                                                                                                                                                        群内有许多来自一线的技术大牛,也有在小厂或外包公司奋斗的码农,我们致力打造一个平等,高质量的Android交流圈子,不一定能短期就让每个人的技术突飞猛进,但从长远来说,眼光,格局,长远发展的方向才是最重要的。

                                                                                                                                                                                        35岁中年危机大多是因为被短期的利益牵着走,过早压榨掉了价值,如果能一开始就树立一个正确的长远的职业规划。35岁后的你只会比周围的人更值钱。

                                                                                                                                                                                        转载请注明:文章转载自 www.mshxw.com
                                                                                                                                                                                        本文地址:https://www.mshxw.com/it/756353.html
                                                                                                                                                                                        我们一直用心在做
                                                                                                                                                                                        关于我们 文章归档 网站地图 联系我们

                                                                                                                                                                                        版权所有 (c)2021-2022 MSHXW.COM

                                                                                                                                                                                        ICP备案号:晋ICP备2021003244-6号