Implementation of the bottom tab of Android development

Source: Internet
Author: User

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

  1. 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>

  2. 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

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.