Android tab tab usage Foundation

In Android programs, tab tab window is a common UI interface element. Its implementation mainly uses the tabhost class.

Tabhost description

Tabhost is a container for label windows.

A tabhost object contains two child element objects:

One object is a tab widget, and users click them to select a specific tab;

The other is the FrameLayout object, which shows the content of the current page.

Child elements are usually controlled through container objects, rather than directly setting the value of child elements.

The following illustrates the usage of tabhost in combination with the example in apidemos.

The first tab example: using tabactivity

This example uses tabactivity.

Java program code:

The layout files are as follows:

Layout file 1

The color string in the layout file is as follows: the text string is omitted.

Operation screenshot:

Note that this class was deprecated in API level 13.

The second program: use tabhost.tabcontentfactory

The tabhost.tabcontentfactory interface is used to create content when a tab is selected, instead of displaying an existing view or starting an activity. Other methods are used.

See code for specific implementation:

Screenshot of program operation:

In addition, the content of tab content can also start another activity, as long as an intent is passed in the setcontent method.

This section will not be introduced again. Please refer to the tabs3.java code in apidemos.

The third program: do not inherit tabacity

In the previous two program examples, both inherit the tabactivity class. If you do not inherit it, you need to write the layout of tabhost, which contains two necessary sub elements: tabwidget and FrameLayout. Their IDs are fixed values. See the code.

Layout file code:

Activity Code:

This method can realize flexible layout, easily add other components, and change the relative position of label bar and content bar.

The fourth program: scrolling tab

When there are too many labels, you need to set the labels into a Scrollview to scroll. With the above program as the basis, this is easy to understand.

Apidemos still gives the method of inheriting tabacity. Here is another method without inheriting tabacity. The two methods are very similar.

Layout file:

Java code:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>