Main code
public class Tabsframe extends Fragmentactivity {
Private Fragmenttabhost Fragmenttabhost;
Private String texts[] = {"Home", "message", "friend", "Square"};
private int imagebutton[] = {R.drawable.selector1,
R.drawable.selector2,
R.drawable.selector3,
R.DRAWABLE.SELECTOR4};
Private Class fragmentarray[] = {Page1.class,
Page2.class,
Page3.class,
Page4.class};
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.tabsframe);
Instantiate Tabhost
Fragmenttabhost = (fragmenttabhost) Findviewbyid (Android. R.id.tabhost);
Fragmenttabhost.setup (this, Getsupportfragmentmanager (), r.id.maincontent);
for (int i = 0; i < texts.length; i++) {
Tabspec Spec=fragmenttabhost.newtabspec (Texts[i]). Setindicator (GetView (i));
Fragmenttabhost.addtab (Spec, fragmentarray[i], NULL);
Set the background (must be after addtab, due to a child node (bottom menu button) otherwise a null pointer exception will occur)
Fragmenttabhost.gettabwidget (). Getchildat (i). Setbackgroundresource (R.drawable.bgselector);
}
}
Private View GetView (int i) {
Get Layout instance
View view=view.inflate (tabsframe.this, r.layout.tabcontent, NULL);
Get Layout Object
ImageView imageview= (ImageView) View.findviewbyid (r.id.image);
TextView textview= (TextView) View.findviewbyid (R.id.text);
Set icon
Imageview.setimageresource (Imagebutton[i]);
Set Title
Textview.settext (Texts[i]);
return view;
}
public boolean onKeyDown (int keycode,keyevent Event) {
if (keycode==keyevent.keycode_back) {
Intent intent=new Intent (tabsframe.this,mainactivity.class);
StartActivity (Intent);
Finish ();
}
return true;
}
}
Layout file
Tabsframe
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:orientation= "Vertical" >
<!--store Main Page Content--
<framelayout
Android:id= "@+id/maincontent"
Android:layout_width= "Fill_parent"
android:layout_height= "0DP"
android:layout_weight= "1" >
</FrameLayout>
<!--bottom menu--
<android.support.v4.app.fragmenttabhost
Android:id= "@android: Id/tabhost"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:background= "@drawable/TAB_BG" >
<framelayout
Android:id= "@android: Id/tabcontent"
Android:layout_width= "0DP"
android:layout_height= "0DP"
android:layout_weight= "0" >
</FrameLayout>
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
Tabcontent
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:gravity= "Center_horizontal"
android:orientation= "Vertical" >
<imageview
Android:id= "@+id/image"
android:layout_height= "Wrap_content"
Android:layout_width= "Wrap_content"
/>
<textview
Android:id= "@+id/text"
Android:padding= "2DP"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:textcolor= "@android: Color/white"
/>
</LinearLayout>
This allows you to implement the bottom menu bar!
This article is from the "Software Learning Summary" blog, be sure to keep this source http://bigcrab.blog.51cto.com/10626858/1689147
Implementation of the bottom tab of Android development