欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android的viewBing初接触

程序员文章站 2022-06-07 18:53:03
...

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

    viewBinding {
        enabled = true

    }
  • 如图
    Android的viewBing初接触

布局文件

Android的viewBing初接触

放置了三个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的名命都是依靠下划线来进行分割的。
Android的viewBing初接触
但是在java代码中调用的时候,却丢失了下划线转化成了驼峰命名。
Android的viewBing初接触
这是因为Android view的优化机制导致的。
如果想要细看的话可以打开自己编译生成的文件。会多出来一个文件就是viewbinding自动生成的文件。
Android的viewBing初接触

// 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));
  }
}

本文参考了以下内容: