如何处理 Android 选项卡和子选项卡

发布于 2024-10-04 22:37:55 字数 323 浏览 1 评论 0原文

我正在寻找创建一个使用选项卡的 Android 应用程序。

要求

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

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

在其中一个选项卡中,我必须使用子选项卡。

就是这样。

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

I am looking to create an Android application that uses Tabs.

Requirements

When a user selects a tab and gets back to the same tab, there shouldn't be a loading period (preferably).

Each tab will contain a listview.

In one of the tabs, I will have to use subtabs.

And that's it.

One great example I really liked (although I didn't try the app) is http://www.usatoday.com/android/.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谜泪 2024-10-11 22:37:55

有两个想法您需要考虑:

1.) 子选项卡不能使用 Activity 来实现,因为 ActivityGroup 无法包装另一个 ActivityGroup

2.) 选项卡中的选项卡大大增加了布局复杂性少量。尤其是关于< 2.0 设备您将很容易遇到 StackOverflowExceptions。 android官方博客上也有一篇关于它的博文,当我找到它时我会更新答案。

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

相关部分是:

由于视图渲染基础设施的变化,布局中不合理的深(超过 10 个左右)或宽(总共超过 30 个)视图层次结构现在可能会导致崩溃。对于过于复杂的布局来说,这始终是一个风险,但您可以认为 Android 1.5 在暴露这个问题方面比 1.1 更好。

考虑到每个 Tabhost 都会增加大约 4 层深度,因此使用双 Tab 很快就会达到 10 层深度。

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

Two thinks you need to consider:

1.) Subtabs cannot be implemented using Activities, since an ActivityGroup cannot wrap another ActivityGroup

2.) Tabs in Tabs blow up Layout complexity quite a bit. Especially on < 2.0 devices you'll easily run into StackOverflowExceptions. There was also a blog post about it on the official android blog, I'll update the answer when I find it.

[EDIT]
There we go: http://android-developers.blogspot.com/2009/04/future-proofing-your-apps.html

The relevant part is:

Due to changes in the View rendering infrastructure, unreasonably deep (more than 10 or so) or broad (more than 30 total) View hierarchies in layouts are now likely to cause crashes. This was always a risk for excessively complex layouts, but you can think of Android 1.5 as being better than 1.1 at exposing this problem.

Considering that every tabhost adds about 4 layers of depth, you'll reach a depth of 10 with double tabs pretty soon.

Keep in mind that "using activities" as tabs is just a sham, they are just additional views within your hierarchy.

梦里梦着梦中梦 2024-10-11 22:37:55

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

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);
    }

NewsList Activity 中的列表视图是由 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 的评论:
是的,请使用可见性属性或尝试使用下面提到的方式实现选项卡:

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);
} 

其中布局文件如下:

    <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 

I just developed a tabbed application and I used the following function to create tabs in code. The function uses intents which mean when ever a tab is selected an Activity is loaded in the tab body using intent. This function is called in main activity.

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);
    }

The list view in NewsList Activity is made by XML as following:

<?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>

In Response to Fabios' comment below:
Yes wither use visibility property or try implementing tabs using the way mentioned below:

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);
} 

Where as Layout file is as below:

    <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 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文