Fragment development of "Android development" 1

Source: Internet
Author: User

I. Introduction of Fragment

1.Fragment appears as part of the activity interface;

2. Multiple fragment can occur simultaneously in one activity, and a fragment may be used in multiple activity;

3. During activity operation, you can add, remove, or replace fragment (add (), remove (), replace ());

4.Fragment can respond to its own input events and have its own life cycle, and of course, their life cycle is directly affected by the activity's life cycle that they belong to.

Then why are we using fragment? The main purpose is to support a more dynamic and flexible UI design on large-screen devices, such as tablets. Tablet screens are much larger than phones, have more room to put more UI components, and create more interaction between these components. We can think of fragment as "small activity", fragment more concise.

Second, the simple use of fragment

So let's simply show 2 fragment as an example to explain.

    

1. Add the fragment in the XML:

New Fragment1, Fragment2 (note that the android.app.Fragment fragment are imported here):

Fragment1 Code:

1  Packagecom.example.fragment;2 3 Importandroid.app.Fragment;4 ImportAndroid.os.Bundle;5 ImportAndroid.util.Log;6 ImportAndroid.view.LayoutInflater;7 ImportAndroid.view.View;8 ImportAndroid.view.ViewGroup;9 Ten ImportCOM.EXAMPLE.FRAGMENTDEMO.R; One  A  Public classFragment1extendsFragment { - @Override -      PublicView Oncreateview (layoutinflater inflater, ViewGroup container, the Bundle savedinstancestate) { -LOG.E ("TAG", "in"); -         returnInflater.inflate (R.layout.fragment1, container,false); -     } +}
View Code

Fragment2 Code:

1  Packagecom.example.fragment;2 3 Importandroid.app.Fragment;4 ImportAndroid.os.Bundle;5 ImportAndroid.view.LayoutInflater;6 ImportAndroid.view.View;7 ImportAndroid.view.ViewGroup;8 9 ImportCOM.EXAMPLE.FRAGMENTDEMO.R;Ten  One  Public classFragment2extendsFragment { A @Override -      PublicView Oncreateview (layoutinflater inflater, ViewGroup container, - Bundle savedinstancestate) { the         returnInflater.inflate (R.layout.fragment2, container,false); -     } -}
View Code

XML code for FRAGMENT1:

1<?xml version= "1.0" encoding= "Utf-8"?>2<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"3Android:layout_width= "Match_parent"4android:layout_height= "Match_parent"5Android:background= "#FF69B4"6android:orientation= "Vertical" >7 8<TextView9Android:layout_width= "Wrap_content"Tenandroid:layout_height= "Wrap_content" Oneandroid:layout_gravity= "Center" AAndroid:text= "This is the first fragment"/> -  -</LinearLayout>
View Code

XML code for FRAGMENT2:

1<?xml version= "1.0" encoding= "Utf-8"?>2<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"3Android:layout_width= "Match_parent"4android:layout_height= "Match_parent"5Android:background= "#EECBAD"6android:orientation= "Vertical" >7 8<TextView9Android:layout_width= "Wrap_content"Tenandroid:layout_height= "Wrap_content" Oneandroid:layout_gravity= "Center" AAndroid:text= "This is a second fragment"/> -  -</LinearLayout>
View Code

We add two fragment in Activity_main.xml, the code is as follows:

1<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"2Android:layout_width= "Match_parent"3android:layout_height= "Match_parent"4Android:baselinealigned= "false" >5 6<Fragment7Android:id= "@+id/fragment1"8Android:name= "Com.example.fragment.Fragment1"9Android:layout_width= "Wrap_content"Tenandroid:layout_height= "Match_parent" Oneandroid:layout_weight= "1"/> A  -<Fragment -Android:id= "@+id/fragment2" theAndroid:name= "Com.example.fragment.Fragment2" -Android:layout_width= "Wrap_content" -android:layout_height= "Match_parent" -android:layout_weight= "1"/> +  -</LinearLayout>
View Code

The mainactivity code is as follows:

1  PackageCom.example.fragmentdemo;2 3 Importandroid.app.Activity;4 ImportAndroid.os.Bundle;5 6  Public classMainactivityextendsActivity {7 8 @Override9     protected voidonCreate (Bundle savedinstancestate) {Ten         Super. OnCreate (savedinstancestate); One Setcontentview (r.layout.activity_main); A     } -}
View Code

Then run the project to show fragment, below.

2. Add fragment dynamically:

      

      

Fragment development of "Android development" 1

Related Article

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.