Android 1. x and 2. x applies to mobile phones, 3. x is applicable to tablets, and from 4.0, the mobile phone and tablet systems are integrated, making 4. x is applicable to mobile phones and tablets.
I. Fragment
Fragment is an important feature of Android. Its literal meaning is "Fragment". In essence, it allows an Activity to display content in multiple display areas and has its own life cycle, however, his life cycle depends on the life cycle of the affiliated Activity.
1: Lifecycle
650) this. width = 650; "src =" http://img1.51cto.com/attachment/201309/132638229.png "title =" 1354170682_3824.png "alt =" 132638229.png"/>
Through the above picture, we can summarize the following rules:
1) the lifecycle of an Activity directly affects the lifecycle of Fragment.
2) Fragment divides Activity into several parts.
3) Fragment can be in the Activity status and its corresponding status and below. For example, if the Activity is in the stopped status, Fragment can be in the onStop, onDestoryView, and below states.
2. manage and operate Fragment
1) Manage Fragment
Get the management object through getFragmentManager. The main methods are:
FindFragmentByIdint id): Find the Fragment by id.
FindFragmentByTagString tag): used to locate the Fragment
2) Fragment operations
Get the operation object through getTranscation of the FragmentManager object
Add (int containerViewID, Fragment fragment, String tag): add a Fragment object to the Activity.
Show (Fragment fragment): displays hidden Fragment
Remove (Fragment fragment): remove an existing Fragment.
Hide (Fragment fragment): hide the displayed Fragment
Commit (): register this Transcation
2. ActionBar
In version 2.x, The TabHost that implements the navigation style is aligned and Beautified in version 4.x. Originally, the navigation developed with 2. x can run on devices of Version 4. x, but the navigation developed with 4. x cannot run on devices of version 2. x.
One purpose: Make a navigation label
The navigation designed by ActionBar can adapt to the screen resolution and avoid adaptation issues. The specific implementation steps are as follows:
1) Implement the ActionBar. TabListener interface, and rewrite several methods to respond to the user's operation of switching navigation labels. That is to say, a tag registers a listener and adds a Fragment to the Activity to complete an operation.
2) instantiate the ActionBar. Tab object. An object is a tag. You can call the set series method to set the tag style and operations.
3) Call addTab () to add the tag to the navigation ActionBar.
3. Create Navigation Based on the two features
The following figure shows how to add a navigation bar to an Activity. When you click a navigation bar, the displayed content is displayed in different regions of the Activity.
1: interface Layout
The main interface is a LinearLayout with an id for the layout. For the other two interfaces, you can add any controls as needed. Here, TextVIew and ImageView are added ).
Main Interface layout:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" tools:context=".MainActivity" ></LinearLayout> |
Layout 1: textfragment. xml
<?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:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /></LinearLayout> |
Layout 2: imagefragment. xml
<?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" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /></LinearLayout> |
2: Fragment class
The number of conversion interfaces is the number of classes inherited from Fragment. Rewrite the onCreateView method, load the xml file to be displayed on the corresponding interface, and obtain the View on the interface.
Area 1: TextFragment. java
// Inherit from Fragmentpublic class TextFragment extends Fragment {private View view; // declare the View @ Override public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {// load the layout of the corresponding interface to obtain the View view = inflater. inflate (R. layout. textfragment, container, false); // return View return view ;}} |
Area 2: ImageFragment. java
// Inherit from Fragmentpublic class ImageFragment extends Fragment {// declare the View private View imageview; @ Override public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {// load the layout, returned imageview = inflater. inflate (R. layout. imagefragment, container, false); return imageview ;}} |
3: Main Interface
Create navigation, navigation tag objects, set monitoring of navigation tags to respond to operations, and add tags to navigation.
Public class MainActivity extends Activity {// declare the navigation object private ActionBar actionBar; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // load the view setContentView (R. layout. activity_main); // instantiate the navigation object actionBar = getActionBar (); // you can specify the display style of the navigation bar. setNavigationMode (ActionBar. NAVIGATION_MODE_TABS); // create the first region TextFragment textFragment = new TextFragment (); // instantiate the first tag object Tab tab1 = actionBar. newTab (); tab1.setIcon (R. drawable. ic_launcher); // set the label's icon style tab1.setText ("Tab 1"); // set the text tab1.setTabListener (new TextListener (textFragment) displayed by the icon )); // Add the corresponding listening actionBar to the tag. addTab (tab1); // Add the tag to the navigation bar // create the second region ImageFragment imageFragment = new ImageFragment (); // create the second Tab tab2 = actionBar. newTab (); tab2.setIcon (R. drawable. ic_launcher); // set the label icon style tab2.setText ("Tab 2"); // set the label text tab2.setTabListener (new IamgeListener (imageFragment )); // Add the corresponding listening actionBar to the tag. addTab (tab2); // Add tags to the navigation} // The class TextListener implements android that listens to the first region in the internal class form. app. actionBar. tabListener {private TextFragment textFragment; // declare the region // constructor public TextListener (TextFragment textFragment) {this. textFragment = textFragment;} // three methods to be rewritten // The method triggered when the label is re-selected @ Override public void onTabReselected (Tab tab, FragmentTransaction ft) {}// method triggered when the tag is selected @ Override public void onTabSelected (Tab tab, FragmentTransaction ft) {ft. add (R. id. layout, textFragment, null); // Add a region to the layout of the Activity} // method triggered when the label is deselected @ Override public void onTabUnselected (Tab tab, FragmentTransaction ft) {ft. remove (textFragment); // remove region} // implement class IamgeListener implements android listening for the second region. app. actionBar. tabListener {private ImageFragment imageFragment; // declare the region // constructor public IamgeListener (ImageFragment imageFragment) {this. imageFragment = imageFragment;} // method triggered when the label is re-selected @ Override public void onTabReselected (Tab tab, FragmentTransaction ft) {}// method triggered when the tag is selected @ Override public void onTabSelected (Tab tab, FragmentTransaction ft) {ft. add (R. id. layout, imageFragment, null); // Add a region to the layout of the Activity} // method triggered when the label is deselected @ Override public void onTabUnselected (Tab tab, FragmentTransaction ft) {ft. remove (imageFragment); // remove region }}} |
4: Result
Similar to the navigation in 2.x, different tabs are displayed at the bottom.
It's about to crash... 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/0936213061-1.gif "alt =" j_0046.gif "/>
This article is from the "Schindler" blog, please be sure to keep this source http://cinderella7.blog.51cto.com/7607653/1299181