Use of fragment (i)

Source: Internet
Author: User

This blog is mainly about how to use fragment.

?

The steps to use fragment are similar to the steps for customizing the view:

    1. Defining a layout file for fragment
    2. Subclasses that implement extended fragment
    3. The view is generated from the XML layout file in the Oncreateview () method of the extended subclass.
    4. The extension subclass of fragment is referenced in the mainactivity layout file.
    5. This way, you can use fragment to run the program.

      ?

      The code is as follows (note that the development environment used is Android Studio 1.2):

      ?

      Fragmeng1 Layout file:

      ?

<? XML version="1.0 " encoding="Utf-8" ?>
< linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"
Android : orientation= "Vertical" Android : Layout_width= "Match_parent"
android: layout_height="match_parent" android: background= "#00FF00"
>

<TextView
Android : Layout_width= "Match_parent"
Android : layout_height= "Wrap_content"
Android : text= "This is Fragment1."
Android : textcolor= "#000000"
Android : textsize= "25SP"

/>

</LinearLayout>

?

Fragment1 The corresponding Java class:

?

 Packagecom.cm.myfragment;

Importandroid.app.Fragment;
ImportAndroid.os.Bundle;
Importandroid.support.annotation.Nullable;
ImportAndroid.view.LayoutInflater;
ImportAndroid.view.View;
ImportAndroid.view.ViewGroup;

/**
* Created by Administrator on 2016/1/1.
*/
Public classFragment1extendsfragment{

@Nullable
@Override
PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {
returninflater.inflate (r.layout.Fragment1, Container,false);

}
}

?

Fragment2 Layout file:

?

<? XML version="1.0 " encoding="Utf-8" ?>
< linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"
Android : orientation= "Vertical" Android : Layout_width= "Match_parent"
android: layout_height="match_parent" android: background= "#FFFE00"
>
<TextView
Android : Layout_width= "Match_parent"
Android : layout_height= "Wrap_content"
Android : text= "This is Fragment2."
Android : textsize= "25SP"
Android : textcolor= "#000000"

/>
</LinearLayout>

?

Java class for Fragment2:

?

 Packagecom.cm.myfragment;

Importandroid.app.Fragment;
ImportAndroid.os.Bundle;
Importandroid.support.annotation.Nullable;
ImportAndroid.view.LayoutInflater;
ImportAndroid.view.View;
ImportAndroid.view.ViewGroup;

/**
* Created by Administrator on 2016/1/1.
*/
Public classFragment2extendsfragment{
@Nullable
@Override
PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {
returninflater.inflate (r.layout.Fragment2, Container,false);
}
}

?

Mainactivity Layout file:

?

<LinearLayout xmlns: Android = "Http://schemas.android.com/apk/res/android"
xmlns: Tools = "Http://schemas.android.com/tools" Android : Layout_width= "Match_parent"
android: layout_height="match_parent"
>

<Fragment
Android : Name= "Com.cm.myfragment.Fragment1"
Android : id= "@+id/fragment1"
Android : Layout_width= "0DP"
Android : layout_weight= "1"
android: layout_height="match_parent"
/>

<Fragment
Android : Name= "Com.cm.myfragment.Fragment2"
Android : id= "@+id/fragment2"
Android : Layout_width= "0DP"
Android : layout_weight= "1"
android: layout_height="match_parent"
/>


</LinearLayout>

?

Mainactivity Java class: No changes have been made.

?

To run the program:

?

?

Use of fragment (i)

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.