Android development: Fragment completely parses fragment_main.xml/activity_main.xml

Source: Internet
Author: User

Note: The original source cannot be indicated.

We all know that the interface display on Android is implemented through Activity, which is too common. I believe everyone is familiar with it and I will not repeat it here.

However, Activity also has its limitations. The same interface may be nice to display on the mobile phone, but it may not be good on the tablet, because the screen of the tablet is very large, when the mobile phone interface is placed on the tablet, the screen may be too long or the control spacing is too large. In this case, the better experience is to embed "small Activity" in the Activity, and then each "small Activity" can have its own layout. Therefore, Fragment, the main character of Alibaba Cloud, is on stage today.

Fragment

In order to make the interface better displayed on the tablet, Android 3.0 introduces the Fragment function, which is very similar to Activity and can contain layout like Activity. Fragment is usually used in nested activities. Now imagine this scenario: There are two Fragment, Fragment 1 contains a ListView, and each row displays the title of a book. Fragment 2 contains TextView and ImageView to display the detailed content and image of the book.

If the program is currently running in portrait mode on a tablet or mobile phone, Fragment 1 may be embedded in one Activity, and Fragment 2 may be embedded in another Activity, as shown in:

If the program is currently running on a horizontal screen tablet, two Fragment instances can be embedded in the same Activity, as shown in:

From this we can see that using Fragment allows us to make full use of the screen space of the tablet. Next we will explore how to use Fragment together.

First note that Fragment was introduced in version 3.0. If you are using a system earlier than version 3.0, You need to first import the jar package of the android-support-v4 to use the Fragment function.

Create a project named Fragments and create a layout file named fragment1.xml in the layout Folder:

[Html] view plaincopy

As you can see, this layout file is very simple. There is only one LinearLayout, and a TextView is added to it. Let's create another fragment2.xml:

[Html] view plaincopy

Create a new class Fragment1, which inherits from Fragment:

[Java] view plaincopy

We can see that this class is also very simple. It mainly loads the fragment1.xml layout file we just wrote and returns it. In the same way, let's write Fragment2 again:

[Java] view plaincopy

Open or create activity_main.xml as the layout file of the main Activity, add two Fragment references to it, and use the android: name prefix to reference the specific Fragment:

[Html] view plaincopy

Finally, open or create a MainActivity as the main Activity of the program. The code in it is very simple and is automatically generated:

[Java] view plaincopy

Now we can run the program once, and we can see that an Activity contains two Fragment elements, which divide the entire screen equally, as shown below:

Add Fragment dynamically

You have learned how to use Fragment in XML, but this is only the simplest function of Fragment. Fragment is really powerful in that it can be dynamically added to the Activity, so this is something you must master. When you learn to add Fragment to the Activity while the program is running, the interface of the program can be customized in a more diverse manner. Now let's take a look at how to dynamically add Fragment.

Modify the Code on the basis of the previous section, open activity_main.xml, delete all references to Fragment, only keep the LinearLayout of the outermost layer, and add an id to it, because we need to dynamically add Fragment without adding it in XML, the code after deletion is as follows:

[Html] view plaincopy

Open MainActivity and modify the Code as follows:

[Java] view plaincopy

First, we need to get the width and height of the screen, and then judge, if the screen width is greater than the height, add fragment1, if the height is greater than the width, add fragment2. Dynamic Fragment addition is mainly divided into four steps:

1. Get the FragmentManager, which can be obtained directly through getFragmentManager in the Activity.

2. Start a transaction by calling the beginTransaction method.

3. Add Fragment to the container, which is generally implemented using the replace method. You need to input the container id and Fragment instance.

4. Submit the transaction and call the commit method to submit the transaction.

Run the program, as shown in the following figure:

 

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.