更改 TabHost 内的视图(一个活动,多个视图)

发布于 2024-10-11 00:28:44 字数 2283 浏览 2 评论 0原文

现在我在我的应用程序中使用具有单一活动和单独视图的 tabhost。

选项卡活动:

    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,用户单击城市名称,选项卡的内容被酒店列表替换,用户选择一家酒店并始终在同一选项卡,酒店信息已加载。

我怎样才能实现这个场景?布局充气机?

提前致谢。

right now I'm using in my application tabhost with single activity and separate views.

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

Layout

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

And things are working quite well as long as i'm using one level depth. Unfortunately now I need to make one of the tab to work like following:

Tab loading ListView with the list of cities, User clicks on city name, tab's content is replaced with the list of the Hotels, user choose a hotel and always in the the same tab, hotel's info is loaded.

How can i achieve this scenario? LayoutInflater?

Thanks in advance.

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

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

发布评论

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

评论(1

吻泪 2024-10-18 00:28:44

您可以使用 Intent 将 Activity 设置为选项卡内容,并且此 Activity 可以调用其他 Activity,无论是 listView 还是任何其他 Activity。

/** 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);

现在,在加载的活动中,您可以使用这样的代码,

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

这将适合您。

you can use intent to set Activity as your tab content, and this activity can call other activities, either listView or any other.

/** 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);

Now in the loaded activity you can use the code like

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

this will work for you.

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