前段時間Google發(fā)現(xiàn)AndroidStudio3.6.1正式版抠璃,這次更新帶來了很多新特性民镜,其中一個值得學(xué)習(xí)的就是ViewBinding
什么是ViewBinding
In most cases, view binding replaces findViewById.
引用官方的說法是踩叭,在大部情況下锨苏,使用ViewBinding替換findViewById耿币。以前如果不使用第三方框架梳杏, 不管怎樣封裝,使用起來還是很不方便(比如找不到控件淹接、控件ID沖突等問題)十性;
怎樣使用ViewBinding
使用條件:
1. Android Studio 3.6 Canary 11+;
2. Gradle插件3.6.1+塑悼;
準(zhǔn)備好了嗎劲适??厢蒜?
在Activity在使用ViewBinding
- 模塊的build.xml中添加ViewBinding的支持霞势;
android {
...
viewBinding {
enabled = true
}
}
在項目中開啟了ViewBinding后,就會為每一個布局文件生成一對應(yīng)的綁定文件郭怪,如果不需要生成綁定文件支示,只需要在根布局添加:tools:viewBindingIgnore="true":
<LinearLayout
...
tools:viewBindingIgnore="true" >
...
</LinearLayout>
- 添加一個布局文件activity_main.xml,代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="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">
<TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
- 添加一個ActivityMainActivity.java,代碼如下
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding viewBinding;//ActivityMainBinding就是上面activity_main.xml生成的綁定文件
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//以為是這樣綁定布局文件的
// setContentView(R.layout.activity_main);
//現(xiàn)在是這樣綁定布局文件的
viewBinding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(viewBinding.getRoot());
//在這里設(shè)置一個顯示文件
viewBinding.tvContent.setText("這里是通過ViewBinding設(shè)置的文本");
}
}
使用起來還是比較簡單的鄙才,運行一下程序看看效果:
image.png
在Fragment在使用ViewBinding
- 先添加一個布局文件fragment_view_binding.xml颂鸿,代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="center"
android:text="Hello World!"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_weight="1" />
</androidx.constraintlayout.widget.ConstraintLayout>
- 添加一個FragmentViewBindingFragment,代碼如下:
public class ViewBindingFragment extends Fragment {
private FragmentViewBindingBinding binding;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
binding = FragmentViewBindingBinding.inflate(inflater, container, false);
View view = binding.getRoot();
binding.tvContent.setText("這里是在Fragment通過ViewBinding設(shè)置的文本");
return view;
}
public static ViewBindingFragment newInstance() {
Bundle args = new Bundle();
ViewBindingFragment fragment = new ViewBindingFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
}
- 把上面的activity_main.xml攒庵,修改一下嘴纺,用來添加Fragment,修改后代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="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">
<TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="center"
android:text="Hello World!"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@id/f_layout"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_weight="1" />
<View
android:id="@+id/v_line"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray"
app:layout_constraintBottom_toTopOf="@+id/f_layout"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_content" />
<FrameLayout
android:id="@+id/f_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_content"
app:layout_constraintVertical_weight="1" />
</androidx.constraintlayout.widget.ConstraintLayout>
- 同時把上面的Activity也修改一下浓冒,修改后代碼如下:
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding viewBinding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
viewBinding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(viewBinding.getRoot());
viewBinding.tvContent.setText("這里是通過ViewBinding設(shè)置的文本");
//添加fragment
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction tx = fm.beginTransaction();
tx.replace(R.id.f_layout, ViewBindingFragment.newInstance());
tx.commit();
}
}
運行一下程序看看效果:
image.png
在Fragment中使用也是比較簡單的栽渴,如注意的時,在Fragment中使用ViewBinding時稳懒,要在onDestroyView()方法中把binding的引用移除闲擦,不然會引起內(nèi)存泄漏;
總結(jié):
- 使用起來比較方便,編譯比較快墅冷,而且是官方推薦的纯路;
- 目前還不支持布局變量和布局表達(dá)式;
- 不支持雙向綁定寞忿,所以只適合一些簡單的使用場境驰唬;