我想在 android 中创建标签,但我真的不知道如何创建,
只是下载了一些视频,但这真的没有帮助我使用 TabActivity 不幸的是它已被弃用:( 所以我需要你帮助我谢谢建议
问问题
3604 次
1 回答
0
您必须设置一个 FragmentActivity 主类来运行 Fragments。
您需要使用支持库 android.support.v4.app 中提供的 FragmentTabHost 类
使用 FragmentTabHost 的类:
public class SampleTabFragment extends Fragment {
private FragmentTabHost mTabHost;
public static View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// fragment not when container null
if (container == null) {
return null;
}
// inflate view from layout
view = (LinearLayout) inflater.inflate(R.layout.offer_tabs, container, false);
// Setting Up the TabHost
mTabHost = (FragmentTabHost) view.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.offer_realtabcontent);
// For adding a tab divider image
mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);
// For Setting up the Tabs with their respective labels
mTabHost.addTab(mTabHost.newTabSpec("info").setIndicator(createTabView
(mTabHost.getContext(), "INFO")), DetailedOfferFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("location").setIndicator(createTabView
(mTabHost.getContext(), "LOCATION")), LocationFragment.class, null);
setRetainInstance(true);
return view;
}
/***** Method for Custom TextView for Tabs *****/
private static View createTabView(final Context context, final String text) {
View view = LayoutInflater.from(context).inflate(R.layout.tabs_text_bg, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
return view;
}
}
使用选项卡的布局 offer_tabs.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#6D6C6C"
android:orientation="vertical" >
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#696969" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
<FrameLayout
android:id="@+id/offer_realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
于 2013-08-14T13:04:35.743 回答