【问题标题】:How to get a view from an activity, which is located inside a tab, while the view is located outside the tab如何从位于选项卡内的活动中获取视图,而视图位于选项卡外
【发布时间】:2011-05-11 12:53:15
【问题描述】:

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

<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 (并且操作栏在它的旁边),但是我仍然需要访问操作栏,因为图标所有当前可用的操作都位于那里。

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

【问题讨论】:

    标签: android android-tabhost tabwidget android-actionbar


    【解决方案1】:

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

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

    【讨论】:

    • 非常感谢 - 这正是我想要的。
    • ActionBar 不继承自 View,因此这只会使您的应用程序崩溃。这将如何解决任何问题?
    【解决方案2】:

    您将获得操作栏视图。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-24
      • 1970-01-01
      • 2014-07-28
      • 1970-01-01
      • 1970-01-01
      • 2012-08-14
      • 1970-01-01
      相关资源
      最近更新 更多