草庐IT

android - 更改 TabHost 内的 View (一个 Activity ,多个 View )

coder 2023-12-24 原文

现在我在我的应用程序 tabhost 中使用单个 Activity 和单独的 View 。

选项卡 Activity :

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TabHost tabs = (TabHost) findViewById(R.id.tabhost);        
    tabs.setup();

    TabHost.TabSpec spec = tabs.newTabSpec(TAB_HOTELS);
    spec.setContent(R.id.tab1);
    spec.setIndicator("Hotels");
    tabs.addTab(spec);

    spec = tabs.newTabSpec(TAB_LAST_MINUTE);
    spec.setContent(R.id.tab2);
    spec.setIndicator("Last Minute");
    tabs.addTab(spec);

布局

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabhost" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout android:orientation="vertical"
        android:layout_width="fill_parent" android:layout_height="fill_parent">
        <TabWidget android:id="@android:id/tabs"
            android:layout_width="fill_parent" android:layout_height="wrap_content" />
        <FrameLayout android:id="@android:id/tabcontent"
            android:layout_width="fill_parent" android:layout_height="fill_parent">
            <LinearLayout android:layout_width="fill_parent"
                android:layout_height="fill_parent" android:id="@+id/tab1"
                android:orientation="vertical">
                <ListView android:id="@+id/listaLocalita"
                    android:layout_width="fill_parent" android:layout_height="wrap_content" />
            </LinearLayout>

            <LinearLayout android:layout_width="fill_parent"
                android:layout_height="fill_parent" android:id="@+id/tab2"
                android:orientation="vertical" android:paddingTop="60px">
                <TextView android:layout_width="fill_parent"
                    android:layout_height="100px" android:text="This is tab 2"
                    android:id="@+id/txt2" />
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>

只要我使用一级深度,一切都运行良好。不幸的是,现在我需要让其中一个选项卡像下面这样工作:

用城市列表加载 ListView 的选项卡,用户单击城市名称,选项卡的内容替换为酒店列表,用户选择酒店并始终在同一选项卡中,加载酒店的信息。

我怎样才能实现这种情况?布局膨胀器?

提前致谢。

最佳答案

你可以使用intent将Activity设置为你的tab内容,这个activity可以调用其他的activity,listView或者任何其他的都可以。

/** tid1 is firstTabSpec Id. Its used to access outside. */
        TabSpec MainTab = tabHost.newTabSpec("tag1");
        TabSpec SearchTab = tabHost.newTabSpec("tag2");
        TabSpec MessageTab = tabHost.newTabSpec("tag3");
        TabSpec ServicesTab = tabHost.newTabSpec("tag4");
        TabSpec SettingsTab = tabHost.newTabSpec("tag5");

        MainTab.setIndicator("Main", res.getDrawable(R.drawable.anchor_36));
        MainTab.setContent(new Intent(this, MainView.class));

    SearchTab.setIndicator("Search", res.getDrawable(R.drawable.clock_36));
    SearchTab.setContent(new Intent(this, SearchView.class));

    MessageTab.setIndicator("Messages", res
            .getDrawable(R.drawable.dialog_36));
    MessageTab.setContent(new Intent(this, MessagesView.class));

    ServicesTab.setIndicator("Services", res
            .getDrawable(R.drawable.cloud_36));
    ServicesTab.setContent(new Intent(this, ServicesView.class));

    SettingsTab.setIndicator("Settings", res
            .getDrawable(R.drawable.settings_36));
    SettingsTab.setContent(new Intent(this, SettingsView.class));

    /** Add tabSpec to the TabHost to display. */
    tabHost.addTab(MainTab);
    tabHost.addTab(SearchTab);
    tabHost.addTab(MessageTab);
    tabHost.addTab(ServicesTab);
    tabHost.addTab(SettingsTab);

现在在加载的 Activity 中,您可以使用如下代码

StartActivity(new intent(currentActivity.this, newActivity.class));

这对你有用。

关于android - 更改 TabHost 内的 View (一个 Activity ,多个 View ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4650672/

有关android - 更改 TabHost 内的 View (一个 Activity ,多个 View )的更多相关文章

  1. ruby-on-rails - Ruby on Rails 迁移,将表更改为 MyISAM - 2

    如何正确创建Rails迁移,以便将表更改为MySQL中的MyISAM?目前是InnoDB。运行原始执行语句会更改表,但它不会更新db/schema.rb,因此当在测试环境中重新创建表时,它会返回到InnoDB并且我的全文搜索失败。我如何着手更改/添加迁移,以便将现有表修改为MyISAM并更新schema.rb,以便我的数据库和相应的测试数据库得到相应更新? 最佳答案 我没有找到执行此操作的好方法。您可以像有人建议的那样更改您的schema.rb,然后运行:rakedb:schema:load,但是,这将覆盖您的数据。我的做法是(假设

  2. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  3. ruby-on-rails - 在 Ruby 中循环遍历多个数组 - 2

    我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代

  4. ruby - 使用 Vim Rails,您可以创建一个新的迁移文件并一次性打开它吗? - 2

    使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta

  5. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  6. ruby-on-rails - 渲染另一个 Controller 的 View - 2

    我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>

  7. ruby - 多个属性的 update_column 方法 - 2

    我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2

  8. ruby-on-rails - 项目升级后 Pow 不会更改 ruby​​ 版本 - 2

    我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby​​版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby​​版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘

  9. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  10. ruby - Capistrano 3 在任务中更改 ssh_options - 2

    我尝试使用不同的ssh_options在同一阶段运行capistranov.3任务。我的production.rb说:set:stage,:productionset:user,'deploy'set:ssh_options,{user:'deploy'}通过此配置,capistrano与用户deploy连接,这对于其余的任务是正确的。但是我需要将它连接到服务器中配置良好的an_other_user以完成一项特定任务。然后我的食谱说:...taskswithoriginaluser...task:my_task_with_an_other_userdoset:user,'an_othe

随机推荐