Tab总是在TabHost中选中

时间:2016-04-01 20:28:24

标签: android android-fragments android-tabhost fragment-tab-host

我的TabHost存在问题,在片段内,第一个要选择的标签将始终突出显示,因此我经常选择两个标签,如下所示

first image, notifications tabs is highlighted when it should not

此处不应突出显示通知!

但问题仅适用于通知标签,因为您可以在此处看到标签朋友的行为正常,因为它是第二个添加..

Secend picture here, friends works normally

以下是我的TabHost

片段的代码
public class ProfileContentFragment extends Fragment {
private FragmentTabHost mTabHost;
public ProfileContentFragment() {
}

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.profile_content_fragment,container, false);


    mTabHost = (FragmentTabHost)     rootView.findViewById(android.R.id.tabhost);
    mTabHost.setup(getActivity(), getFragmentManager(),      android.R.id.tabcontent);

              mTabHost.addTab(mTabHost.newTabSpec("notifications").setIndicator("Notifications    "),
            NotificationsFragment.class, null);

    mTabHost.addTab(mTabHost.newTabSpec("friends").setIndicator("Friends"),
            FriendsFragment.class, null);

    return rootView;
}

@Override
public void onDestroyView(){
    super.onDestroyView();;
    mTabHost=null;
}
}

这是XML

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

<android.support.v4.app.FragmentTabHost
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
            android:id="@+id/LinearLayout01"
            android:orientation="vertical"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent">

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_height="wrap_content"
                android:layout_width="fill_parent">
            </TabWidget>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_height="fill_parent"
                android:layout_width="fill_parent">
            </FrameLayout>

        </LinearLayout>


</android.support.v4.app.FragmentTabHost>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

已解决:

通过更改

解决了这个问题
 mTabHost.setup(getActivity(), getFragmentManager(),     android.R.id.tabcontent);

致:

mTabHost.setup(getActivity(), getChildFragmentManager(), android.R.id.tabcontent);