Modify Activity Layout
If you enable the activity to support data Binding, add the "<layout>" tag to the outermost layer of the layout, and because it is added to the outermost layer, the modifications are simple even if you refactor the existing project without affecting the existing layout structure.
The following is an example of mainactivity.
To modify the layout of the pre-activty_main.xml:
<?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="match_parent" android:orientation="vertical" android:gravity="center" > <TextView android:id="@+id/tv_example" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" /></LinearLayout>
The layout of the modified Activty_main.xml:
<?xml version="1.0" encoding="utf-8"?><layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <data> </data> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" > <TextView android:id="@+id/tv_example" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" /> </LinearLayout></layout>
The elements of the <data> tag here are the data required by the page, the following article we will talk about, you can also click here to see, here we put aside for a moment, focusing on the use of data binding activity.
Binding in activity
After we have modified the layout, we can do data binding in the code below. The Java binding file for the layout is automatically generated in the project: Activitymainbinding. Careful observation will find that the file name is the layout of the underscore form into the Java specification of the Hump form, followed by binding.
Below we enter the Mainactivity.java in the following data binding operations.
If the IDE does not automatically generate the corresponding binding class correctly when you write the code, refer to this article: Android Studio does not normally generate a workaround for a related class/method, and only takes a few steps to enable the IDE to generate normally:
- To define a member variable:
private ActivityMainBinding mBinding;
- Initialize the mbinding in OnCreate ()
@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main); mBinding.tvExample.setText("Binding Text");}
At this point, you can get all the view in the layout from the mbinding, such as the mbinding.tvexample above.
Summarize
The use of data binding in activity is simple, eliminating the template code Findviewbyid (), and avoiding the use of third-party libraries such as Butterknife, saving time and effort.
In addition to getting the elements in the layout, we'll talk about how to set up the data in the layout, and click here to view it.
Android development Tutorial-Using Data Binding (iii) use in activity