This blog is mainly about how to use fragment.
?
The steps to use fragment are similar to the steps for customizing the view:
- Defining a layout file for fragment
- Subclasses that implement extended fragment
- The view is generated from the XML layout file in the Oncreateview () method of the extended subclass.
- The extension subclass of fragment is referenced in the mainactivity layout file.
- 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)