Android development Tutorial-Using Data Binding (iii) use in activity

Source: Internet
Author: User

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:

    1. To define a member variable:
private ActivityMainBinding mBinding;
    1. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.