gpt4 book ai didi

android - 如何使用 TabActivity 或 FragmentActivity 在 android 中创建选项卡

转载 作者:太空狗 更新时间:2023-10-29 15:09:27 26 4
gpt4 key购买 nike

我想在 android 中创建标签,但我真的不知道如何创建和
刚刚下载了一些视频,但这真的没有用不幸的是,我使用了 TabActivity,它已被弃用:(所以我需要你帮助我谢谢你的任何建议

最佳答案

您必须设置一个 FragmentActivity 主类来运行 fragment 。

您需要使用支持库 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>

关于android - 如何使用 TabActivity 或 FragmentActivity 在 android 中创建选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18232278/

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