【发布时间】:2017-03-27 08:12:40
【问题描述】:
我已经设法在我的fragment 上添加了TabHost,但是当我点击一个标签时,我希望另一个fragment 进入FrameLayout,另一个也是如此。
我已尝试应用该方法,但在 setup host.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent) 中不起作用。
有什么办法可以解决这个问题?
这是我的 XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TabHost
android:id="@+id/tabHost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<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="30dp" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</FrameLayout>
</LinearLayout>
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@+id/tabLinear"
/>
</TabHost>
</RelativeLayout>
这是我的 Java 代码:
public class HomeFragment extends Fragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home,
container, false);
TabHost host = (TabHost) view.findViewById(R.id.tabHost);
host.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
host.addTab(host.newTabSpec("tab11").setIndicator("Tab11"), PhotosFragment1.class, null);
host.addTab(host.newTabSpec("tab22").setIndicator("Tab22"), PhotosFragment2.class, null);
return view;
}
}
【问题讨论】:
-
您能否尝试将您的
TabHost替换为FragmentTabHost并致电host.setup(getActivity(), getFragmentManager(), R.id.realtabcontent); -
我第一次使用 FragmentTabHost 但我在布局和 java 中不断收到空指针异常...所以切换到 TabHost
-
正如question 的答案中所述,您应该能够毫无例外地实现它。我也运行良好。
-
渲染过程中引发异常:没有已知标签为 null 的选项卡这是 xml 中发生的情况
-
好吧 tnx 兄弟,当我完美运行它时它对我有用....但是当我看到它仍然显示的 xml 文件时(渲染期间引发的异常:没有标签已知标签为空)
标签: android android-fragments android-tabhost