gpt4 book ai didi

android - TabSpec SetContent 到 fragment

转载 作者:行者123 更新时间:2023-11-29 01:44:26 25 4
gpt4 key购买 nike

我在 Fragment 中使用 TabHost 来创建标签。我遇到的问题是 TabSpec 的 setContent() .我需要设置它,以便它在 <FrameLayout> 下嵌套另一个 fragment .

我是否使用 getChildFragmentManager()去做这个?我该怎么做?

XML 布局:

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

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

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

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

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

</LinearLayout>

fragment 类:

public class ActivityFragment extends Fragment implements TabHost.OnTabChangeListener {
private static final String FOLLOWING_SPEC = "following";
private static final String YOU_SPEC = "you";

private TabHost tabHost;

public ActivityFragment(){}

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

View view = inflater.inflate(R.layout.fragment_activity, container, false);

tabHost = (TabHost)view.findViewById(android.R.id.tabhost);
tabHost.setup();

TabHost.TabSpec followingSpec = tabHost.newTabSpec(FOLLOWING_SPEC);
followingSpec.setIndicator("Following");
followingSpec.setContent(); // <===Need to set content to fragment

TabHost.TabSpec youSpec = tabHost.newTabSpec(YOU_SPEC);
youSpec.setIndicator("You");

tabHost.addTab(followingSpec);
tabHost.addTab(youSpec);
tabHost.setCurrentTab(0);

return view;
}

@Override
public void onTabChanged(String s) {

}
}

最佳答案

答案在这里。我将 android 的 TabHost 与支持库的 FragmentTabHost 混合在一起。两种不同的东西!

public class ActivityFragment extends Fragment implements TabHost.OnTabChangeListener {
private static final String FOLLOWING_SPEC = "following";
private static final String YOU_SPEC = "you";

private FragmentTabHost tabHost;

public ActivityFragment(){}

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

View view = inflater.inflate(R.layout.fragment_activity, container, false);

tabHost = (FragmentTabHost)view.findViewById(R.id.tabhost);
tabHost.setup(getActivity(), getChildFragmentManager(), android.R.id.tabcontent);

tabHost.addTab(tabHost.newTabSpec(FOLLOWING_SPEC).setIndicator("Following"), FollowingActivityFragment.class, null);
tabHost.addTab(tabHost.newTabSpec(YOU_SPEC).setIndicator("You"), YouActivityFragment.class, null);
tabHost.setCurrentTab(0);

return view;
}



@Override
public void onTabChanged(String s) {

}
}

关于android - TabSpec SetContent 到 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22294444/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com