开发者

How to get a view from an activity, which is located inside a tab, while the view is located outside the tab

I have a layout which inludes actionbar and tabs:

<TabHost xmlns:android="http://schemas.and开发者_高级运维roid.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>

And on the actionbar I have several buttons that I need to be able to access from activities within tabs. But findViewById returns null for any view located on the actionbar - I find this quite logical, because for any activity inside tab their contentRoot is FrameLayout (and actionbar is ourside of it), but nevertheless I need an access to the actionbar, because icons for all currently available actions are located there.

I also have thought of passing actionbar as extra to intents for all activities inside tabs, but View is not serializable nor Parcelable, so it can't be passed as extra.


Let TabActivity provide access to actionbar. Child activities should request actionbar using the following code, e.g.:

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


You will get action bar view.

int resId = getResources().getIdentifier("action_bar_container", "id", "android");
View actionbarView =  getWindow().getDecorView().findViewById(resId);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜