This article comes from the legend of the Fat Treasure Road , the reference must be indicated by the source!
This chapter is a long time. Originally wanted to write the tab, learned Tabhost,tabwidget, put the code to prepare the study, found unexpectedly in 4.0.3 version number was discarded.
Baidu a bit, hair now behind the version number, with Fragmenttabhost and Layoutinflater to replace. There are some content about frame on the internet, but it is not a beginner's tutorial.
Not written in plain enough. Want to directly take the code down to study, found that very many people are uploading code snippets, and then give a fee link. As a poor cock, I can only study it by myself at 1.1 o ' walk.
What is Frament? The literal translation is the meaning of the fragment, and Frament is a part of the activity in order to solve the problem that the Android same set of code displays on devices of different size screens.
In fact, the interface is divided into a part of the convenient management. More in-depth understanding can be seen here click Open link
Fragmentactivity inherits from activity. You can use activity for frament related content.
Fragmenttabhost replaces the Tabhost class.
Here, let's look at a picture:
Uh..
。 The MacBook figure is playing.
。。
Look at the code side to say it:
Activity_hello_world.xml the layout file of the main interface, using the frame layout, to send the menu bar and the page together, the same time display
<?XML version= "1.0" encoding= "Utf-8"?
><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Fill_ Parent " android:layout_height=" fill_parent " android:orientation=" vertical "> <framelayout android:id= "@+id/realtabcontent" android:layout_width= "fill_parent" android:layout_height= "0dip" android:layout_weight= "1"/> <android.support.v4.app.fragmenttabhost android:id= "@android: ID /tabhost " android:layout_width=" fill_parent " android:layout_height=" Wrap_content " android: background= "@drawable/bottom_bar" > <framelayout android:id= "@android: Id/tabcontent" Android:layout_width= "0DP" android:layout_height= "0DP" android:layout_weight= "0"/> </ Android.support.v4.app.fragmenttabhost></linearlayout>
We can see that the menu bar requires a layout file to control the placement of these buttons. At the same time, within the tab page, there is a need for a layout file.
Tab_item_view.xml the Tabbutton layout file. button consists of two parts, picture ImageView and text TextView
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" wrap_content " android:layout_height=" wrap_content " android:gravity= "Center" android:orientation= "vertical" > <imageview android:id= "@+id/imageview" android: Layout_width= "Wrap_content" android:layout_height= "wrap_content" android:focusable= "false" Android :p adding= "3DP" android:src= "@drawable/bottom_bar" > </ImageView> <textview Android:id= "@+id/textview" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "" android:textsize= "12SP" android:textcolor= "#FFFFFF" > </TextView> </LinearLayout>
Fragment1.xml page Fragment layout file c1ffc1 is the background color.
<?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:o rientation= "vertical" android:background= "#C1FFC1" ></LinearLayout>
Fragment2.xml, this is just a different color.
Android:background= "#EEEE00"
Fragment3.xml, this is just a different color.
Android:background= "#FFFFFF"
Fragment4.xml, this is just a different color.
Android:background= "#C6555D"
Fragment5.xml, this is just a different color.
Android:background= "#000000"
Fragment1.java Read the layout file
Package Com.fable.helloworld;import Com.fable.helloworld.r;import Com.fable.helloworld.r.layout;import Android.os.bundle;import Android.support.v4.app.fragment;import Android.view.layoutinflater;import Android.view.view;import Android.view.ViewGroup; public class Fragment1 extends fragment{ @Override public View Oncreateview (layoutinflater inflater, ViewGroup Container, Bundle savedinstancestate) { return inflater.inflate (r.layout.fragment1, NULL);// Fragment2345 is actually different here. }}
Helloworldactivity.java main Logic implementation
Package Com.fable.helloworld; Import Android.os.bundle;import Android.support.v4.app.fragmentactivity;import Android.support.v4.app.fragmenttabhost;import Android.view.layoutinflater;import Android.view.View;import Android.widget.imageview;import Android.widget.tabhost.tabspec;import Android.widget.textview;import Com.fable.helloworld.fragment1;import Com.fable.helloworld.r;public class Helloworldactivity extends fragmentactivity {//fragmentactivity is able to operate the fragment activity private fragmenttabhost mtabhost; Layout filler private layoutinflater mlayoutinflater; Fragment Array interface private Class mfragmentarray[] = {fragment1.class, fragment2.class, Fragment3.class, Fragment4.clas S, fragment5.class}; Store image array private int mimagearray[] = {r.drawable.home,r.drawable.ic_dialog_email, R.drawable.ic_menu_my_calendar, R.drawable.ic_search_category_default, R.drawable.ic_input_add}; tab text Private String mtextarray[] = {"Home", "message", "friend", "search", "many others"}; public void OnCreate (BundLe Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_hello_world); Initview ();//Initialize View}/** * Initialize component */private void Initview () {mlayoutinflater = Layoutinflater.from (this);//Layout fill , the dynamic layout is used with mtabhost = (fragmenttabhost) Findviewbyid (Android. R.id.tabhost)///Find Tabhost tab Mtabhost.setup (this, Getsupportfragmentmanager (), r.id.realtabcontent);//tab Container//Get FR Number of agment int count = mfragmentarray.length; for (int i = 0; i < count; i++) {//sets the icon, text, and content for each Tabbutton tabspec tabspec = Mtabhost.newtabspec (mtextarray [i]). Setindicator (Gettabitemview (i)); Add Tabbutton to Tab tab Mtabhost.addtab (Tabspec, mfragmentarray[i], NULL);//The second parameter is the details of the corresponding page of the tab//set Tabbutton back King Mtabhost.gettabwidget (). Getchildat (i). Setbackgroundresource (R.drawable.bottom_bar); }}/** * * Set icon and text for each Tabbutton */private View gettabitemview (int index) {View view = Mlayoutinflater.infla Te (r.layouT.tab_item_view, NULL),//tab dynamic layout ImageView ImageView = (ImageView) View.findviewbyid (R.id.imageview); Imageview.setimageresource (Mimagearray[index]);//set icon TextView TextView = (TextView) View.findviewbyid ( R.id.textview); Textview.settext (Mtextarray[index]);//Set the text//can be seen here, to a view of the inside of the insertion of things. Is the first through the ID to find the element object, and then the object to operate the return view; }}
Not much code. But the file is a bit more, so upload the code, click the Open link
Android Starter--fragmenttabhost Implementation tab and menu