小编Pab*_*ito的帖子

Android studio:如何使用 ViewPager 添加标签

我想在片段中添加一个带有 pagerview(可滚动)的选项卡。

    public class MyFragment extends Fragment {
    private FragmentTabHost tabHost;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        tabHost = new FragmentTabHost(getActivity());
        tabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
        tabHost.addTab(tabHost.newTabSpec("one").setIndicator("One"), OneFragment.class, null);
        tabHost.addTab(tabHost.newTabSpec("two").setIndicator("Two"), TwoFragment.class, null);
        return tabHost;
    }


    @Override
    public void onDestroyView(){
        super.onDestroyView();
        tabHost=null;
    }
}
Run Code Online (Sandbox Code Playgroud)

有了这个布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.app.FragmentTabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <android.support.v4.view.ViewPager
            android:id="@+id/realtabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>

    </LinearLayout>
</android.support.v4.app.FragmentTabHost>
Run Code Online (Sandbox Code Playgroud)

我尝试了几种解决方案,但都不起作用。我需要使用片段,而不是片段活动。编写的代码工作。

tabs android fragment scrollable pageviews

1
推荐指数
1
解决办法
7375
查看次数

标签 统计

android ×1

fragment ×1

pageviews ×1

scrollable ×1

tabs ×1