美好的一天。我有一些问题。 我的应用程序的 ListView 在左侧,详细 View 在右侧。在右 View 中,我有一个带有选项卡主机的 fragment 。但我还想为所有选项卡添加 Activity 。 例如: 我左边有客户名单。 右侧有选项卡:“客户评论”、“客户照片”、“客户信息” 在客户评论中,我需要在 Activity 中对此客户发表评论,并可以添加新评论。 我已经制作了 ListView 和详细信息,但我在将选项卡主机集成到其中时遇到问题。所以我有什么。这是我的详细代码 fragment
public class ItemDetailFragment extends Fragment {
public static final String ARG_ITEM_ID = "item_id";
DummyContent.DummyItem mItem;
private Activity lo_parentAct;
public ItemDetailFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments().containsKey(ARG_ITEM_ID)) {
mItem = DummyContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_item_detail, container, false);
if (mItem != null) {
Intent lv_intent;
// ((TextView) rootView.findViewById(R.id.item_detail)).setText(mItem.content);
TabHost tabHost=(TabHost)rootView.findViewById(R.id.tabHost);
tabHost.setup();
TabSpec spec1=tabHost.newTabSpec("Tab 1");
spec1.setIndicator("Общая информация");
lo_parentAct = this.getActivity();
lv_intent = new Intent(lo_parentAct, ClientInfoActivity.class);
TabSpec spec2=tabHost.newTabSpec("Tab 2");
spec2.setIndicator("Заметки");
lv_intent = new Intent(lo_parentAct, ClientCommentsActivity.class);
TabSpec spec3=tabHost.newTabSpec("Tab 3");
spec3.setIndicator("Фото");
lv_intent = new Intent(lo_parentAct, ClientPhotosActivity.class);
tabHost.addTab(spec1);
tabHost.addTab(spec2);
tabHost.addTab(spec3);
}
return rootView;
}
}
布局
<TabHost android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tabHost"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TabWidget
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@android:id/tabs"
/>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/tabcontent"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/tab1"
android:orientation="vertical"
android:paddingTop="60px"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="100px"
android:text="This is tab1"
android:id="@+id/txt1"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tab2"
android:orientation="vertical"
android:paddingTop="60px"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="100px"
android:text="This is tab 2"
android:id="@+id/txt2"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tab3"
android:orientation="vertical"
android:paddingTop="60px"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="100px"
android:text="This is tab 3"
android:id="@+id/txt3"
/>
</LinearLayout>
</FrameLayout>
</TabHost>
最佳答案
add tab host with activities inside fragment
您不能将 Activity 分成 fragment !
据我了解 - 您想将具有不同 activities
的 TabActivity
(已弃用)放入主要 Activity 的根 Fragment
- 这种方式绝对是错误的。
实现您需要的一种方法是:
- 创建一个
FragmentActivity
。 - 将 TabWidget 放入
FragmentActivity
的根布局中。 - 然后将不同的
Fragments
放入TabWidget的选项卡中。
您可以在另一个theme (link)中查看我的类似答案以及代码示例。 .
关于java - 在 fragment 内添加带有 Activity 的选项卡主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12400461/