我想制作一个底部有 3 个标签的应用程序。在我的应用程序中,我希望每个选项卡单击都会打开另一个具有 ViewPager 的 FragmentActivity,因此我可以用手指滑动活动,但仍保留在同一个选项卡中。现在,每个选项卡只打开一个片段,所以我不能使用查看寻呼机。如何从 FragmentTabHost 打开片段活动?这是我的代码,谢谢!
public class MainActivity extends FragmentActivity {
private FragmentTabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bottom_tabs);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
Bundle b = new Bundle();
b.putString("key", "one");
mTabHost.addTab(mTabHost.newTabSpec("one").setIndicator("one"),
Fragment1.class, b);
//
b = new Bundle();
b.putString("key", "two");
mTabHost.addTab(mTabHost.newTabSpec("two")
.setIndicator("two"), Fragment2.class, b);
b = new Bundle();
b.putString("key", "three");
mTabHost.addTab(mTabHost.newTabSpec("three").setIndicator("three"),
Fragment3.class, b);
}
这只是一个片段示例
public class Fragment1 extends Fragment {
private TextView text;
public Fragment1() {
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = LayoutInflater.from(getActivity()).inflate(R.layout.layout,
null);
text = (TextView) v.findViewById(R.id.text);
if (getArguments() != null) {
//
try {
String value = getArguments().getString("key");
text.setText("THIS IS THE FIRST TAB - " + value);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
}
//
}
这是XML
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" >
</FrameLayout>
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:background="@android:color/darker_gray"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
</android.support.v4.app.FragmentTabHost>