带有固定标题Android的标签

时间:2014-08-13 13:25:46

标签: android android-fragments

我需要一个固定区域(大约20%)在顶部,然后在它下方的3个标签。 xml是

mylist.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.2"
        android:orientation="vertical" >

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.05"
            android:visibility="invisible" />

        <TextView
            android:id="@+id/list_name"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.3"
            android:textColor="@color/loclistitem_text"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/list_spec"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.3" />

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.1"
            android:visibility="invisible" />

        <TextView
            android:id="@+id/list_user"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.2" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.8"
        android:orientation="horizontal" >

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.05"
            android:visibility="invisible" />

        <FrameLayout
            android:id="@+id/list_realtabcontent"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.95" />

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.05"
            android:visibility="invisible" />
    </LinearLayout>

    <android.support.v4.app.FragmentTabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#CCF8FB" >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dip"
            android:layout_height="0dip"
            android:layout_weight="0" />
    </android.support.v4.app.FragmentTabHost>

</LinearLayout>

Main Fragment定义了其中的3个标签

MyListFragment
    public class MyListFragment extends Fragment {

        private FragmentTabHost mTabHost;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // TODO Auto-generated method stub

            View rootView = inflater.inflate(R.layout.mylist, container,
                    false);
            TextView name = (TextView) rootView.findViewById(R.id.list_name);
            TextView spec = (TextView) rootView.findViewById(R.id.loc_spec);
            TextView users = (TextView) rootView.findViewById(R.id.list_user);
            name.setText(AppSession.getInstance().getData().getName());
            mTabHost = new FragmentTabHost(getActivity());

            mTabHost.setup(getActivity(), getChildFragmentManager(),
                    R.id.list_realtabcontent);

            Bundle arg1 = new Bundle();
            arg1.putInt("Tab1", 1);

            mTabHost.addTab(
                    mTabHost.newTabSpec("Tab1").setIndicator("Tab1",
                            getResources().getDrawable(R.drawable.tab_left)),
                    Tab1.class, arg1);

            Bundle arg2 = new Bundle();
            arg2.putInt("Tab2", 2);
            mTabHost.addTab(
                    mTabHost.newTabSpec("Tab2").setIndicator("Tab2",
                            getResources().getDrawable(R.drawable.tab_middle)),
                    Tab2.class, arg2);

            Bundle arg3 = new Bundle();
            arg3.putInt("Tab3", 3);
            mTabHost.addTab(
                    mTabHost.newTabSpec("Tab3").setIndicator("Tab3",
                            getResources().getDrawable(R.drawable.tab_rigth)),
                    Tab3Fragment.class, arg2);

            return mTabHost;

        }

        @Override
        public void onDetach() {
            super.onDetach();

            try {
                Field childFragmentManager = Fragment.class
                        .getDeclaredField("mChildFragmentManager");
                childFragmentManager.setAccessible(true);
                childFragmentManager.set(this, null);

            } catch (NoSuchFieldException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }

    }

mylist.xml中定义的固定布局未显示,因为我在MyListFragment类中指定了return mTabHost。它正确显示所有3个选项卡和所需列表。但要显示标题,如果我将其更改为return rootView,我会收到错误。

我在过去做过类似的事情,但后来我没有在标签顶部修复标题。在网上搜索了很多,但运气不好。请指教。

0 个答案:

没有答案