Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Tab function in Android

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report