gpt4 book ai didi

java - 在片段内创建选项卡布局

转载 作者:行者123 更新时间:2023-12-02 04:07:21 24 4
gpt4 key购买 nike

我有一个来自 Android studio 的简单抽屉导航,我对其进行了升级。我的问题是如何在一个片段中放置一个选项卡?

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?
}

提前致谢。请教我,我是新手

最佳答案

尝试这样做来处理选项卡:

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;
}
}

布局:

<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>

托管 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();


}

和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"
/>

在此之后,只需创建普通片段 B 和 C 等类。结果将是:

enter image description here

关于java - 在片段内创建选项卡布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34146831/

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