1.启用Viewbinding功能:在模块build.gradle文件android节点下添加如下代码
android {
......
buildFeatures {
viewBinding true
}
}
完整的build.gradle文件内容如下:
plugins {
id 'com.android.application'
}
android {
namespace 'com.example.myapplication001'
compileSdk 32
defaultConfig {
applicationId "com.example.myapplication001"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
buildFeatures {
viewBinding true
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'com.github.bumptech.glide:glide:3.7.0'
implementation 'com.github.andyoom:citypicker:v1.0.4'
implementation 'pub.devrel:easypermissions:3.0.0'
//Glide图片加载库
implementation 'com.github.bumptech.glide:glide:4.11.0'
implementation 'com.github.chrisbanes:PhotoView:2.3.0'
implementation fileTree(dir: "libs", includes: ["*.jar", "*.aar"])
}
重新编译后系统会为每个布局文件生成对应的Binding类。该类中包含对应布局中具有 ID 的所有视图的直接引用。
生成类的目录在模块根目录/build/generated/data_binding_base_class_source_out下。如下图:
2.在Activity中使用
在MainActivity布局文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="come on" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
MainActivity中使用:
ActivityMainBinding mViewBinding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mViewBinding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(mViewBinding.getRoot());
mViewBinding.message.setText("这是ViewBinding设置的文本");
...
}
使用的时候在Activity的onCreate方法里调用其静态inflate方法,返回ViewBinding实例,通过ViewBinding实例可以直接访问布局文件中带id的控件,比如上面的TextView,mViewBinding.message
如果想在生成绑定类时忽略某个布局文件,将
xmlns:tools=“http://schemas.android.com/tools”
tools:viewBindingIgnore=“true”
属性添加到相应布局文件的根视图中。
完整的布局文件内容如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
tools:viewBindingIgnore="true"
android:orientation="vertical">
<TextView
android:id="@+id/tv_1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="left|center_vertical"
android:paddingLeft="20dp"
android:text="A"
android:textSize="24sp" />
</LinearLayout>
3.在Fragment中使用
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
FragmentViewBindBinding binding = FragmentViewBindBinding.inflate(inflater, container, false);
return binding.getRoot();
}
4.在Dialog中的使用
ublic class MyDialog extends Dialog {
protected View mView;
protected DialogBottomBinding mBinding;
public MyDialog(@NonNull Context context, @StyleRes int themeResId) {
super(context, themeResId);
//原来的写法
mView = View.inflate(getContext(), getLayoutId(), null);
//使用ViewBinding的写法
mBinding = DialogBottomBinding.inflate(getLayoutInflater());
mView = mBinding.getRoot();
setContentView(mView);
}
}
5.在 Adapter 中的使用
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
ItemViewBinding viewBinding = ItemViewBinding.inflate(LayoutInflater.from(mContext));
return new MyViewHolder(viewBinding);
}
6.其他具体的使用方法,参考这篇文章:http://events.jianshu.io/p/80f15a34b73e