代码之家  ›  专栏  ›  技术社区  ›  Fabio Milheiro

如何接近Android标签和子标签

  •  0
  • Fabio Milheiro  · 技术社区  · 14 年前

    我希望创建一个使用标签的Android应用程序。

    要求

    当用户选择一个选项卡并返回到同一个选项卡时,不应该有加载周期(最好)。

    每个选项卡将包含一个列表视图。

    在其中一个选项卡中,我将不得不使用子选项卡。

    就这样。

    我非常喜欢的一个很好的例子是(虽然我没有尝试这个应用程序) http://www.usatoday.com/android/ .

    2 回复  |  直到 14 年前
        1
  •  1
  •   U.P    14 年前

    我刚刚开发了一个标签应用程序,并使用以下函数在代码中创建标签。该函数使用intent,这意味着每当选择一个选项卡时,都会使用intent在选项卡主体中加载一个活动。此函数在主活动中调用。

    private void SetupTabs()
        {
            TabHost tabHost = getTabHost();
    
    
            TabHost.TabSpec spec;  
            Intent intent;  
    
            //*****************************
            intent = new Intent().setClass(MPaper.this, NewsList.class);        
            spec = tabHost.newTabSpec(NewsTag).setIndicator("News")
                          .setContent(intent);        
            tabHost.addTab(spec);
            //*****************************
            intent = new Intent().setClass(MPaper.this, Search.class);        
            spec = tabHost.newTabSpec(SearchTag).setIndicator("Search")
                          .setContent(intent);        
            tabHost.addTab(spec);
            //****************************
            intent = new Intent().setClass(MPaper.this, MyNewsList.class);        
            spec = tabHost.newTabSpec(MyNewsTag).setIndicator("My News")
                          .setContent(intent);        
            tabHost.addTab(spec);
            //*****************************
            intent = new Intent().setClass(MPaper.this, Extras.class);        
            spec = tabHost.newTabSpec(ExtrasTag).setIndicator("Extras")
                          .setContent(intent);        
            tabHost.addTab(spec);
            //*****************************
    
    
            tabHost.setCurrentTabByTag(NewsTag);
        }
    

    “新闻列表”活动中的列表视图由XML生成,如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tabhost" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    
        <ListView 
            android:id="@+id/NewsList" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            >
    
        </ListView>
    
    </LinearLayout>
    

    针对Fabios的以下评论: 是的,请使用visibility属性或尝试使用下面提到的方法实现选项卡:

    private void TabSetup()
        {
            tabs = (TabHost)findViewById(R.id.tabhost);        
            tabs.setup(); 
            TabHost.TabSpec spec = tabs.newTabSpec("MainNewsTag");
            spec.setContent(R.id.MainNews);
            spec.setIndicator("Home");
            tabs.addTab(spec);
    
    
    
    
            spec = tabs.newTabSpec("SearchTag");
            spec.setContent(R.id.Search);
            spec.setIndicator("Search");
            tabs.addTab(spec);
    
    
            spec = tabs.newTabSpec("MyNewsTag");
            spec.setContent(R.id.MyNews);
            spec.setIndicator("My News");
            tabs.addTab(spec);
    
            spec = tabs.newTabSpec("ExtrasTag");
            spec.setContent(R.id.MyNews);
            spec.setIndicator("Extras");
            tabs.addTab(spec);
    } 
    

    其中as布局文件如下:

        <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/tabhost" android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <RelativeLayout android:layout_height="fill_parent"
                android:layout_width="fill_parent">
                <TableLayout android:layout_height="fill_parent" 
                   android:layout_width="fill_parent">
                   <TabWidget android:id="@android:id/tabs"
                   android:layout_height="wrap_content"
                       android:layout_width="fill_parent" />
                   <FrameLayout android:id="@android:id/tabcontent"
                        android:layout_height="wrap_content"
                                    android:layout_width="fill_parent" >  
                       <ListView android:id="@+id/MainNews" 
                                 android:layout_width="fill_parent"
                                 android:layout_height="wrap_content">
                       </ListView>
    .... and so on 
    
        2
  •  2
  •   Timo Ohr    14 年前

    两个人认为你需要考虑:

    1.)无法使用活动实现子选项卡,因为 ActivityGroup 无法包装另一个 活动组

    2)标签中的标签会大大增加布局的复杂性。尤其是在<2.0设备上,您很容易遇到 StackOverflowExceptions . 安卓官方博客上也有一篇关于它的博文,我找到答案后会更新。

    [编辑] 我们开始了: http://android-developers.blogspot.com/2009/04/future-proofing-your-apps.html

    相关部分为:

    由于视图呈现基础结构的更改,布局中不合理的深度(超过10个左右)或宽度(总共超过30个)视图层次结构现在可能会导致崩溃。对于过于复杂的布局来说,这总是一个风险,但是你可以认为Android 1.5在暴露这个问题上优于1.1。

    考虑到每一个tabhost增加了大约4层深度,很快就可以使用双tab达到10层深度。

    请记住,“使用活动”作为选项卡只是一种伪装,它们只是层次结构中的附加视图。