To create a tab UI, you must use a TabHost and a TabWidget. TabHost must be the root node of the layout file, it contains TabWidget for displaying tabs and FrameLayout for displaying option content. to create a tab UI, you need to use a TabHost and a TabWidget, tabHost must be the root node of the layout file. it contains the TabWidget for displaying the tab and a FrameLayout for displaying the option content.
You can use one or two methods to implement your tab content: use the option card in an Activity to switch between views, or use the option card to change all separated activities. You can use the method you want in the program according to your needs. However, if each tab provides a unique user Activity, it makes sense to implement an independent Activity for each tab, you 'd better manage your applications in your discrete group, instead of using a large number of applications and layout files.
In this example, you can create a tab for each Activity to create a tab UI
1. start a new project called HelloTabWidget
2. First, create three independent Activity programs in your project: ArtistsActivity, AlbumsActivity, and SongsActivity. each of them represents a separate tab, currently, TextView is used to display a simple information for each program, for example:
package org.hualang.tabwidget; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class AlbumsActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView textview = new TextView(this); textview.setText("This is the Albums tab"); setContentView(textview); } }
package org.hualang.tabwidget; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class SongsActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView textview = new TextView(this); textview.setText("This is the Songs tab"); setContentView(textview); } }
package org.hualang.tabwidget; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class ArtistsActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView textview = new TextView(this); textview.setText("This is the Artists tab"); setContentView(textview); } }
Note that in this example, you do not need to layout a file. you only need to create a TextView and assign a value to the text. Create three similar activities again and register them in the AndroidManifest. xml file. Otherwise, an error is returned.
3. you need to set an icon for each tab. each icon has two versions. one is when the tab is selected, and the other is when the tab is not selected. In general design, we recommend that you use the gray color when selected, and the unselected one should be white, for example
② Create an ic_tab_artists.xml file in the res/drawable/directory and insert the following information:
In the preceding file, when the status of the tab changes, the Tab automatically switches between the two defined images.
4. open the res/layout/main. xml file and insert the following information:
This layout file will display the tab weapon to provide navigation between each Activity
TabHost requires a TabWidget and a FrameLayout layout. to make the positions of TabWidget and FrameLayout vertical, a LinearLayout is required. FrameLayout is the content of each tab, the content is empty because every Activity is automatically embedded in TahHost.
Note: the ID tag and tabcontent element of TabWidget and FrameLayout elements must be used because TahHost retrieves their references and it exactly expects these names.
6. compile HelloTabWidget. Inherit TabWidget
package org.hualang.tabwidget;import android.app.TabActivity;import android.content.Intent;import android.content.res.Resources;import android.os.Bundle;import android.widget.TabHost;public class HelloTabWidget extends TabActivity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Resources res = getResources(); // Resource object to get Drawables TabHost tabHost = getTabHost(); // The activity TabHost TabHost.TabSpec spec; // Resusable TabSpec for each tab Intent intent; // Reusable Intent for each tab // Create an Intent to launch an Activity for the tab (to be reused) intent = new Intent().setClass(this, ArtistsActivity.class); // Initialize a TabSpec for each tab and add it to the TabHost spec = tabHost.newTabSpec("artists").setIndicator("Artists", res.getDrawable(R.drawable.ic_tab_drawable)) .setContent(intent); tabHost.addTab(spec); // Do the same for the other tabs intent = new Intent().setClass(this, AlbumsActivity.class); spec = tabHost.newTabSpec("albums").setIndicator("Albums", res.getDrawable(R.drawable.ic_tab_drawable)) .setContent(intent); tabHost.addTab(spec); intent = new Intent().setClass(this, SongsActivity.class); spec = tabHost.newTabSpec("songs").setIndicator("Songs", res.getDrawable(R.drawable.ic_tab_drawable)) .setContent(intent); tabHost.addTab(spec); tabHost.setCurrentTab(2); }}
Running result:
The above is the content of the Android UI control series: Tab Layout (Tab Layout). For more information, see PHP Chinese website (www.php1.cn )!