How to add TabBar In Android
Respected All I hav开发者_运维问答e to add TabBar in my application, but i don't know how to add tab bar to application or activity. Can you help me for that.
Thank You Vikram Kadam
You have to use a TabHost
.
You can find a clear example here. Read the tuto for the explanations, here below you have just the code.
In short :
you need an xml file with some layout for each tab, and an activity to display the tabs :
In your xml tabs.xml :
http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
>
<TabHost
id=”@+id/tabs”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
>
<TabWidget
id=”@android:id/tabs”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
/>
<FrameLayout
id=”@android:id/tabcontent”
android:layout_width=”fill_parent”
android:layout_height=”200px”
android:paddingTop=”30px”
>
<LinearLayout
id=”@+id/content1″
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:background=”#ff99ccff”
>
<TextView
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”tab item uno :) ”
/>
</LinearLayout>
<LinearLayout
id=”@+id/content2″
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:background=”#ffffcc99″
>
<TextView
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”tab item dos :/”
/>
<Button
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”tabhost needs”
/>
<Button
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”to be upgraded ;) ”
/>
</LinearLayout>
</FrameLayout>
</TabHost>
</LinearLayout>
In the onCreate
method of your activity :
setContentView(R.layout.tabs);
TabHost tabs = (TabHost)this.findViewById(R.id.tabs);
tabs.setup();
tabs.addTab(“one”, R.id.content1, “labelone”);
tabs.addTab(“two”, R.id.content2, “labeltwo”);
Thanks Jeff for the tuto
精彩评论