1
我想添加一個帶有pagerview(可滾動)選項卡的片段。Android工作室:如何使用ViewPager添加選項卡
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;
}
}
通過這一佈局,
<?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>
我嘗試了幾種解決方案,但不起作用。 我需要使用片段,而不是fragmentActivity。編寫的代碼工作。
使用TabLayout代替TabHost – darwin
檢查本教程http://www.androidhive.info/2015/09/android-material-design-working-with-tabs/ – darwin