如何从视图位于选项卡外部的活动中获取视图,该活动位于选项卡内

时间:2011-05-11 12:53:16

标签: android android-tabhost tabwidget android-actionbar

我有一个包含操作栏和标签的布局:

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout style="@style/BackgroundLayer">

        <!-- Action Bar with buttons -->
        <include layout="@layout/incl_actionbar"/>

        <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"
            android:padding="5dp" />
    </LinearLayout>

</TabHost>

在操作栏上,我有几个按钮,我需要能够从标签中的活动访问。但是findViewById对于位于操作栏上的任何视图都返回null - 我觉得这很合乎逻辑,因为对于tab中的任何活动,他们的contentRoot是FrameLayout(而actionbar是我们的),但是我需要访问操作栏,因为所有当前可用的操作都位于那里。

我还想过将操作栏作为额外内容传递给标签内的所有活动,但是View不是可序列化的也不是Parcelable,所以它不能作为额外的传递。

2 个答案:

答案 0 :(得分:2)

让TabActivity提供对操作栏的访问权限。子活动应使用以下代码请求操作栏,例如:

View actionBar = ((CustomTabActivity) getParent()).getActionBar();

答案 1 :(得分:0)

您将获得操作栏视图。

int resId = getResources().getIdentifier("action_bar_container", "id", "android");
View actionbarView =  getWindow().getDecorView().findViewById(resId);
相关问题