Explanation of Android Fragment (fragmentation ),

Source: Internet
Author: User

Explanation of Android Fragment (fragmentation ),

Fragment literally refers to Fragment. Of course, Fragment aims to solve the serious fragmentation problem of various Android devices. For example, the same App shows good results on mobile phones, however, once the tablet is posted at, it immediately changes its taste. For example, if you are using an android tablet, bei may be more experienced (keng). For this reason, Google officially starts from android 3.0 (corresponding to API 11) fragment is introduced to split the interface into many fragments, and finally splice them according to actual requirements. For example, you can only display the directory list of a book in portrait mode of a mobile phone, however, in Landscape mode (or instant flat mode), you can display the directory list on the left half of the screen, and then display the overview of each list item on the right half of the screen, in this way, the user experience will be much better. Okay. The following describes how to implement a simple Fragment Demo.

The final display effect is as follows: (fragment1 is displayed on the left half of a flat panel and fragment2 is displayed on the right)


1: Create the layout of fragment1 and fragment2 respectively.

Fragment1.xml

<Span style = "font-size: 18px;"> <? 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"> <TextView android: text = "this is fragment1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: textSize = "20sp" android: textColor = "#00CD00"/> </LinearLayout> </span>

Fragment2.xml
<Span style = "font-size: 18px;"> <? 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"> <TextView android: text = "this is fragment2" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: textSize = "20sp" android: textColor = "# EEEE00"/> </LinearLayout> </span>



2: The static interface is declared here. If you want to reference it in the future, you need to create an Activity inherited from Fragment (both fragment1 and fragment2 must be created)

Fragment1.class

<span style="font-size:18px;">package com.example.fragment;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class Fragment1 extends Fragment{   @Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stubreturn inflater.inflate(R.layout.fragment, container,false) ;}}</span>


<span style="font-size:18px;"></span>

Fragment2.class

<span style="font-size:18px;">package com.example.fragment;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class Fragment2 extends Fragment{@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stubreturn inflater.inflate(R.layout.fragment2, container, false) ;}}</span>



3: The two fragments of the base have been implemented. Now you can create a local part of the main interface.

Activity_main.xml

<span style="font-size:18px;"><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"    android:orientation="horizontal"    android:baselineAligned="false"    tools:context="com.example.fragment.MainActivity" >   <fragment        android:id="@+id/fragment1"       android:name="com.example.fragment.Fragment1"       android:layout_height="wrap_content"       android:layout_width="0dp"       android:layout_weight="1"/>       <fragment        android:id="@+id/fragment2"       android:name="com.example.fragment.Fragment2"       android:layout_height="wrap_content"       android:layout_width="0dp"       android:layout_weight="1"/></LinearLayout></span>

Finally, write down the main Activity startup project.
<span style="font-size:18px;">package com.example.fragment;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;public class MainActivity extends ActionBarActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}}</span>


If the project runs, it will not be pasted.





The relationship between API level and version of Android.

Platform Version API Level VERSION_CODE Notes
For Android 4.4 19 KITKAT Platform Highlights
For Android 4.3 18 JELLY_BEAN_MR2 Platform Highlights
Android 4.2, 4.2.2 17 JELLY_BEAN_MR1 Platform Highlights
Android 4.1, 4.1.1 16 JELLY_BEAN Platform Highlights
Android 4.0.3, 4.0.4 15 ICE_CREAM_SANDWICH_MR1 Platform Highlights
Android 4.0, 4.0.1, 4.0.2 14 ICE_CREAM_SANDWICH
For Android 3.2 13 HONEYCOMB_MR2
Android 3.1.x 12 HONEYCOMB_MR1 Platform Highlights
Android 3.0.x 11 HONEYCOMB Platform Highlights
Android 2.3.4
Android 2.3.3
10 GINGERBREAD_MR1 Platform Highlights
Android 2.3.2
Android 2.3.1
For Android 2.3
9 GINGERBREAD
Android 2.2.x 8 FROYO Platform Highlights
Android 2.1.x 7 ECLAIR_MR1 Platform Highlights
Android 2.0.1 6 ECLAIR_0_1
For Android 2.0 5 ECLAIR
For Android 1.6 4 DONUT Platform Highlights
For Android 1.5 3 CUPCAKE Platform Highlights
For Android 1.1 2 BASE_1_1
For Android 1.0 1 BASE



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.