【问题标题】:Get instance of child/sub TabHost from the parent TabHost从父 TabHost 获取子/子 TabHost 的实例
【发布时间】:2013-12-25 15:54:54
【问题描述】:

我创建了一个包含TabHost 的活动。在其中一个tabspec 中有另一个 TabHost(就像一个子 TabHost)。

默认情况下,此 subTabHost 的 visibilitygone,& 仅在选择第二个父 tabspec 时可见。

Now when the second tab is selected, I want to get the instance of the subTabHost inside java code in TabSpec variable.

谢谢

布局:

父 TabHost 布局

<TabHost
        android:id="@android:id/tabhost"
        android:visibility="gone" >
        ...

            <TabWidget
                android:id="@android:id/tabs" />

            <FrameLayout
                android:id="@android:id/tabcontent" >

                <include
                    android:id="@+id/abc"
                    layout="@layout/abc"
                    android:visibility="gone" />

                <include
                    android:id="@+id/subtab2"
                    layout="@layout/subtab2"   <--! sub tab -->
                    android:visibility="gone" />
        ... 


Inside layout of subtab2
<!-- want to get this's tabhost instance in code -->
<TabHost 
        android:id="@android:id/tabhost"
        android:visibility="visible" >

        <LinearLayout
            android:orientation="vertical" >

            <TabWidget
                android:id="@android:id/tabs" />

            <FrameLayout
                android:id="@android:id/tabcontent" >

                <include
                    android:id="@+id/xyz"
                    layout="@layout/xyz"
                    android:visibility="gone" />

        .....

Java 代码

TabHost parent =  mTabHost = (TabHost) findViewById(android.R.id.tabhost);
// How to code below
(if subtab2 is visible)
Tabhost subTabHost = ??

【问题讨论】:

    标签: android android-layout android-tabhost


    【解决方案1】:

    由于您对 TabHosts 使用了相同的 ID,因此您不能在主布局中使用 findViewById(android.R.id.tabhost),但您可以在内容面板中使用它:

    // when the second tab is selected
    FrameLayout content = (FrameLayout) findViewById(android.R.id.tabcontent); // main tabs content
    TabHost subtabs = (TabHost)content.findViewById(android.R.id.tabhost); 
    

    【讨论】:

    • 宾果游戏,正是我需要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多