Tabs on android 4.0 woes

末鹿安然 提交于 2019-12-23 02:32:03

问题


I'm trying to use tabs on android 2.2 - 4.0. I've extended this demo: http://developer.android.com/resources/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentTabs.html with:

TabSpec ts = mTabHost.newTabSpec("randomTab");
Intent intent = new Intent();
intent.setClass(this, FragActiv.class);
ts.setContent(intent);
ts.setIndicator("randomTab");
mTabManager.addTab(ts, FragActiv.class, new Bundle());

But I don't know what to add to onTabChanged, where all the magic happens in the demo.

What I need to do is:
I have a ListFragment and when an item is clicked, it should display details in a separate screen on a small screen/portrait orientation or, on a large screen/landscape orientation, display a fragment with the details on the same screen. Can I work around this with Fragments or should I try to get FragmentActivities working properly?

By following this example: example, I was able to instantiate a FragmentActivity, but it takes up the whole screen, rather than just the area under the tab menu.

Is there any chance of using Activities (or FragmentActivities), not Fragments, as contents of the tabs?


回答1:


There was no need of doing what I wanted (using a TabActivity as content of a tab). I've solved the problem by having two containers inside my tabContent, one for the ListView fragment and one for the details fragment. Here is my layout now:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <HorizontalScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true"
        android:scrollbars="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" />
    </HorizontalScrollView>

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="0" />

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:baselineAligned="false"
        android:fadingEdge="none" >

        <FrameLayout
            android:id="@+android:id/realtabcontent"
            android:layout_width="0px"
            android:layout_height="fill_parent"
            android:layout_weight="1" />

        <FrameLayout
            android:id="@+android:id/details_container"
            android:layout_width="0px"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:visibility="gone" />
    </LinearLayout>
</LinearLayout>

Initially, details_container is GONE. You can use it as following:

        ft.replace(R.id.details_container, detailsFragment);
        FrameLayout detailsLayout = (FrameLayout) mActivity.findViewById(R.id.details_container);
        detailsLayout.setVisibility(View.VISIBLE);


来源:https://stackoverflow.com/questions/11079429/tabs-on-android-4-0-woes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!