[英]Create a Tab layout inside fragment
I have a simple navigation drawer from Android studio build in and I upgrade that. 我有一个内置的Android Studio的简单导航抽屉,我对其进行了升级。 My question is how can I put a tab inside of one fragment?
我的问题是如何将选项卡放在一个片段中?
public class MainFragment extends Fragment {
View v;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_main,container,false);
tabimplement();
return v;
}
public void tabimplement()
{
// WHAT CODE WILL I PUT HERE?
}
Thanks in advance. 提前致谢。 please teach me im newbie here
请在这里教我新手
Try to do this to handle the Tabs: 尝试执行以下操作以处理选项卡:
public class MainFragment extends Fragment {
private FragmentTabHost mTabHost;
//Mandatory Constructor
public MainFragment() {
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tabs,container, false);
mTabHost = (FragmentTabHost)rootView.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("fragmentb").setIndicator("Fragment B"),
FragmentB.class, null);
mTabHost.addTab(mTabHost.newTabSpec("fragmentc").setIndicator("Fragment C"),
FragmentC.class, null);
mTabHost.addTab(mTabHost.newTabSpec("fragmentd").setIndicator("Fragment D"),
FragmentD.class, null);
return rootView;
}
}
With the layout: 使用布局:
<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">
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
The MotherActivity to host the MainFragment: 主持MainFragment的MotherActivity:
public class MotherActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MainFragment fragmenttab = new MainFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.item_detail_container, fragmenttab).commit();
}
And MotherActivity layout: 和MotherActivity布局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/item_detail_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
After this just create the normal fragment B and C, etc Class. 之后,只需创建普通片段B和C等类。 The Result will be:
结果将是:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.