Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize Tab function in Android

Shulou Source: shulou.com Published: 2022-05-31 21:46:33 09月30日 Update

This article mainly introduces "how to realize tab function in Android". In daily operation, I believe many people have doubts about how to realize tab function in Android. Xiaobian consulted all kinds of materials and sorted out simple and easy operation methods. I hope to help you answer the doubts about "how to realize tab function in Android"! Next, please follow the small series to learn together!

First create an Android project and name your Activity HelloTabWidget

Creating a TabHost in main.xml or your own *.xml file requires two elements TabWidget and FrameLayout usually put these two elements in LinearLayout. FrameLayout is used to change content.

Note: TabWidget and FrameLayout have different ID namespaces android:id="@android:id/idnames", which is required for TabHost to find it automatically, and Activity needs to inherit TabActivity.

Android Tab Xml Code

< ?xml version="1.0" encoding="utf-8"?>

< TabHost xmlns:android= "http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent">

< LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">

< TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" />

< FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent">

< TextView android:id="@+id/textview1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="this is a tab" />

< TextView android:id="@+id/textview2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="this is another tab" />

< TextView android:id="@+id/textview3" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="this is a third tab" />

< /FrameLayout>

< /LinearLayout>

< /TabHost>

< ?xml version="1.0" encoding="utf-8"?>

< TabHost xmlns:android= "http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent">

< LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">

< TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" />

< FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent">

< TextView android:id="@+id/textview1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="this is a tab" />

< TextView android:id="@+id/textview2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="this is another tab" />

< TextView android:id="@+id/textview3" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="this is a third tab" />

< /FrameLayout>

< /LinearLayout>

< /TabHost>

Activity needs to inherit TabActivity

Android tab Java code

public class HelloTabWidget extends TabActivity public class HelloTabWidget extends TabActivity

Android tab Java code

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

mTabHost = getTabHost();

mTabHost.addTab(mTabHost.newTabSpec("tab_test1").

setIndicator("TAB 1").setContent(R.id.textview1));

mTabHost.addTab(mTabHost.newTabSpec("tab_test2").

setIndicator("TAB 2").setContent(R.id.textview2));

mTabHost.addTab(mTabHost.newTabSpec("tab_test3").

setIndicator("TAB 3").setContent(R.id.textview3));

mTabHost.setCurrentTab(0);

}

At this point, the study of "how to implement tab functions in Android" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Tags: Function learning code two elements more help different practical next content engineering regular meetings documents articles methods theory knowledge space articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn OPPO Reno Docker Shulou Tech Info Xiaomi