Android advanced components ----- tab, Android ----- Tab

Source: Internet
Author: User

Android advanced components ----- tab, Android ----- Tab

Tabs are commonly used in Android advanced components. They can save pages, such as qq interfaces, contacts, messages, and other function option pages, switching between them does not result in page refresh. For example, the phone functions of mobile phones include contacts, missed calls, and already answered calls. They are all in the same interface, switch between pages on the same interface. If you put the preceding steps on different interfaces, it seems very cumbersome to use them. This section describes how to use tabs to create simple page cutting.

Explanation tab:

The selection card feature gives new users a bit of attention. As a new user, let's talk about it roughly:

For example, an overall tab contains several tabs. The entire tab is the TabHost component, which contains a vertical linear layout. The top part is the tab title (TabWidget ), the lower part is the frame layout. Each time we select to switch pages through the title, the layout of the issued frames will change, and the selected frames will appear at the top. This is the principle of the tab. In terms of code, it is called TabHost as a whole. The title and frame below form TabSpec, And the increase or decrease of the page is actually TabSpec.

1. Add basic layout components (several more linear la s will be added when eclipse is dragged in, which can be deleted, because it is the layout inside the frame layout and does not need to be used)

<TabHost // here is TabHost android: id = "@ android: id/tabhost" android: layout_width = "match_parent" android: layout_height = "match_parent"> <LinearLayout // The vertical linear layout determines the title above. The following figure shows the frame android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <TabWidget // android: id = "@ android: id/tabs" android: layout_width = "match_parent" android: layout_height = "wrap_content"> </TabWidget> <FrameLayout // switch frame android: id = "@ android: id/tabcontent" android: layout_width = "match_parent" android: layout_height = "match_parent"> </FrameLayout> </LinearLayout> </TabHost>

2. Get TabHost and initialize

        private TabHost tabHost;        tabHost =(TabHost)findViewById(android.R.id.tabhost);        tabHost.setup();

3. Create three layout files. These three layout files are added to the frame container of the tab to implement Content Switching.

<?xml version="1.0" encoding="utf-8"?><ImageView xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/n"    android:src="@drawable/nature"    android:layout_width="match_parent"    android:layout_height="match_parent" >    </ImageView>

4. Add a tab for TabHost, including the title and frame. In the main activity, the layout is initialized through setContentView (). The tab is added to the known layout, not the layout initialization, therefore, we need to fill the layout to add the three defined la s to the frame.

LayoutInflater inflater = LayoutInflater. from (this); // gets the layout filler inflater. inflate (R. layout. lay1, tabHost. getTabContentView (); inflater. inflate (R. layout. lay2, tabHost. getTabContentView (); inflater. inflate (R. layout. lay3, tabHost. getTabContentView (); tabHost. addTab (tabHost. newTabSpec ("tab01 "). setIndicator ("Landscape "). setContent (R. id. n); tabHost. addTab (tabHost. newTabSpec ("tab02 "). setIndicator ("Landscape "). setContent (R. id. s); tabHost. addTab (tabHost. newTabSpec ("tab03 "). setIndicator ("START "). setContent (R. id. I ));

Reflection: tabs are simple to implement. The only way to use a layout fill that is hard for beginners to understand is to read more articles.

Please leave a message to help improve it!

 

Related Article

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.