博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android之TabHost使用(引用SDK例子文档)
阅读量:7282 次
发布时间:2019-06-30

本文共 6351 字,大约阅读时间需要 21 分钟。

Tab Layout

To create a tabbed UI, you need to use a TabHost and a TabWidget. The TabHost must be the root node for the layout, which contains both the TabWidget for displaying the tabs and aFrameLayout for displaying the tab content.

You can implement your tab content in one of two ways: use the tabs to swap Views within the same Activity, or use the tabs to change between entirely separate activities. Which method you want for your application will depend on your demands, but if each tab provides a distinct user activity, then it probably makes sense to use a separate Activity for each tab, so that you can better manage the application in discrete groups, rather than one massive application and layout.

In this tutorial, you'll create a tabbed UI that uses a separate Activity for each tab.

  1. Start a new project named HelloTabWidget.
  2. First, create three separate Activity classes in your project: ArtistsActivityAlbumsActivity, and SongsActivity. These will each represent a separate tab. For now, make each one display a simple message using a TextView. For example:
    publicclassArtistsActivityextendsActivity{
        publicvoid onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);         TextView textview =newTextView(this);         textview.setText("This is the Artists tab");         setContentView(textview);     } }

    Notice that this doesn't use a layout file. Just create a TextView, give it some text and set that as the content. Duplicate this for each of the three activities, and add the corresponding<activity/> tags to the Android Manifest file.

  3. You need an icon for each of your tabs. For each icon, you should create two versions: one for when the tab is selected and one for when it is unselected. The general design recommendation is for the selected icon to be a dark color (grey), and the unselected icon to be a light color (white). (See the Icon Design Guidelines.) For example:

     

    For this tutorial, you can copy these images and use them for all three tabs. (When you create tabs in your own application, you should create customized tab icons.)

    Now create a state-list drawable that specifies which image to use for each tab state:

    1. Save the icon images in your project res/drawable/ directory.
    2. Create a new XML file in res/drawable/ named ic_tab_artists.xml and insert the following:
         
         
         
         

      This is a state-list drawable, which you will apply as the tab image. When the tab state changes, the tab icon will automatically switch between the images defined here.

  4. Open the res/layout/main.xml file and insert the following:
       
           
           
       

    This is the layout that will display the tabs and provide navigation between each Activity created above.

    The TabHost requires that a TabWidget and a FrameLayout both live somewhere within it. To position the TabWidget and FrameLayout vertically, a LinearLayout is used. TheFrameLayout is where the content for each tab goes, which is empty now because the TabHost will automatically embed each Activity within it.

    Notice that the TabWidget and the FrameLayout elements have the IDs tabs and tabcontent, respectively. These names must be used so that the TabHost can retrieve references to each of them. It expects exactly these names.

  5. Now open HelloTabWidget.java and make it extend TabActivity:

     

    publicclassHelloTabWidgetextendsTabActivity{
  6. Use the following code for the onCreate() method:
    publicvoid 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 =newIntent().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_artists))                   .setContent(intent);     tabHost.addTab(spec);     // Do the same for the other tabs     intent =newIntent().setClass(this,AlbumsActivity.class);     spec = tabHost.newTabSpec("albums").setIndicator("Albums",                       res.getDrawable(R.drawable.ic_tab_albums))                   .setContent(intent);     tabHost.addTab(spec);     intent =newIntent().setClass(this,SongsActivity.class);     spec = tabHost.newTabSpec("songs").setIndicator("Songs",                       res.getDrawable(R.drawable.ic_tab_songs))                   .setContent(intent);     tabHost.addTab(spec);     tabHost.setCurrentTab(2); }

    This sets up each tab with their text and icon, and assigns each one an Activity.

    A reference to the TabHost is first captured with getTabHost(). Then, for each tab, a TabHost.TabSpec is created to define the tab properties. The newTabSpec(String) method creates a new TabHost.TabSpec identified by the given string tag. For each TabHost.TabSpecsetIndicator(CharSequence, Drawable) is called to set the text and icon for the tab, andsetContent(Intent) is called to specify the Intent to open the appropriate Activity. Each TabHost.TabSpec is then added to the TabHost by calling addTab(TabHost.TabSpec).

    At the very end, setCurrentTab(int) opens the tab to be displayed by default, specified by the index position of the tab.

    Notice that not once was the TabWidget object referenced. This is because a TabWidget must always be a child of a TabHost, which is what you use for almost all interaction with the tabs. So when a tab is added to the TabHost, it's automatically added to the child TabWidget.

  7. Now open the Android Manifest file and add the NoTitleBar theme to the HelloTabWidget's <activity> tag. This will remove the default application title from the top of the layout, leaving more space for the tabs, which effectively operate as their own titles. The <activity> tag should look like this:
  8. Run the application.

Your application should look like this (though your icons may be different):

转载地址:http://gdkjm.baihongyu.com/

你可能感兴趣的文章
Lintcode38 Search a 2D Matrix II solution 题解
查看>>
C# 文件下载 : WebClient
查看>>
snmp v3 的安全配置 snmp 认证与加密配置
查看>>
机械硬盘格式化了的文件寻回办法
查看>>
win10扩容C盘后分区不见的资料如何找到
查看>>
使用composer 实现自动加载
查看>>
网络工程师成长日记373-李宁公司项目
查看>>
PHP递归获取二维数组中指定key的值
查看>>
戴尔R720服务器CPLD version : 103
查看>>
以太坊分片Sharding FAQ
查看>>
How to fix onSubmit data validation once and for a
查看>>
Java 11 已发布,String 还能这样玩!
查看>>
spark入门介绍(菜鸟必看)经验技术分享
查看>>
计算机网页设计中布局与排版的主要方法---上海天象网络技术有限公司
查看>>
MariaDB数据库存储引擎、索引和EXPLAIN
查看>>
TDSQL 全时态数据库系统--核心技术
查看>>
同进公司,为什么别人升职比你快?
查看>>
阿里p8程序员吐槽:新来应届生都开卡宴上班,我真的是不甘心啊!
查看>>
本地apt
查看>>
你可能不知道的JavaScript代码片段和技巧(上)
查看>>