1

我有一个包含操作栏和选项卡的布局:

<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 - 我觉得这很合乎逻辑,因为对于选项卡内的任何活动,它们的 contentRoot 是 FrameLayout (并且操作栏在它的旁边),但是我仍然需要访问操作栏,因为图标所有当前可用的操作都位于那里。

我还考虑过将操作栏作为选项卡内所有活动的额外意图传递,但 View 不是可序列化的,也不是 Parcelable,因此不能作为额外传递。

4

2 回答 2

2

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

View actionBar = ((CustomTabActivity) getParent()).getActionBar();
于 2011-05-11T13:07:22.930 回答
0

您将获得操作栏视图。

int resId = getResources().getIdentifier("action_bar_container", "id", "android");
View actionbarView =  getWindow().getDecorView().findViewById(resId);
于 2015-06-30T11:17:54.990 回答