gpt4 book ai didi

java - 创建 FragmentTabHost 导致一些麻烦

转载 作者:行者123 更新时间:2023-11-29 03:31:19 27 4
gpt4 key购买 nike

所以我正在开发一个应用程序,直到现在,我一直在使用带有两个 Activity 的普通选项卡布局。现在有人告诉我,使用 FragmentTabHost 会更好。所以我一直在阅读一些 docs这是我到目前为止想出的:

MainActivity_Fragment:

public class MainActivity_Fragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.activity_main, container, false);
}

}

RecordedLibrary_Fragment:

public class RecordedLibrary_Fragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.activity_recorded_library, container, false);
}
}

TabLayout Activity :

public class TabLayout extends FragmentActivity {

//GLOBAL VARIABLES///////////////
private FragmentTabHost mTabHost;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_layout);

mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), r.id....); // <-- problem here

mTabHost.addTab(mTabHost.newTabSpec("Recording").setIndicator("Recording"),
MainActivity_Fragment.class, null);

mTabHost.addTab(mTabHost.newTabSpec("Library").setIndicator("Library"),
RecordedLibrary_Fragment.class, null);
}
}

现在这就是我被困的地方。有几件事没有在文档中解释。所以我有两个问题。

文档中的这一行:

mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

他们从哪里得到 R.id.realtabcontent 的?

还有一件事没有解释;选项卡 Activity 的 xml 应该如何显示?

我当前的选项卡布局 xml:

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>

如您所见,选项卡布局 xml 仍然来自旧选项卡布局。需要调整成FragmentTabLayout还是这样好?

最佳答案

Now someone told me that it's much better to do it usning FragmentTabHost.

好吧,我做了 sayFragmentTabHost 可能是这 3 个中最不受欢迎的一个,尽管它最接近您当前的代码。”

Where did they get R.id.realtabcontent ?

这是布局中小部件的 ID。具体来说,它将在 R.layout.fragment_tabs 中,文档忽略了显示。

幸运的是,这似乎来自支持包示例,并且可以找到该布局 here ,在撰写本文时的定义为:

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

<TabWidget
android:id="@android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>

<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>

<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>

</LinearLayout>
</android.support.v4.app.FragmentTabHost>

And another thing that isn't explained; how should the xml of the tab activity look?

见上文。

Do I need to adjust it to FragmentTabLayout or is it good this way?

好吧,如果不出意外,您需要将 TabHost 替换为 FragmentTabHost

但是,坦率地说,FragmentTabHost 在文档方面比我在评论您之前的问题时记得的更糟糕。

关于java - 创建 FragmentTabHost 导致一些麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18137121/

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