如何在Android中使用TabActivity或FragmentActivity创建标签 [英] How to Create Tab in android using TabActivity or FragmentActivity

查看:154
本文介绍了如何在Android中使用TabActivity或FragmentActivity创建标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建Android的标签,但我真的不知道如何创建和结果
刚下载一些视频但实际上这是没有帮助
我用TabActivity不幸的是它的德precated :(
所以,我需要你来帮助我感谢您的建议


解决方案

您必须设置一个FragmentActivity主类,运行碎片。

您需要使用现有的FragmentTabHost类支持库andr​​oid.support.v4.app <​​/ P>

有关使用FragmentTabHost类:

 公共类SampleTabFragment扩展片段{私人FragmentTabHost mTab​​Host;
公共静态视图视图;@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
        捆绑savedInstanceState){    //片段而不是当容器空
    如果(集装箱== NULL){
        返回null;
    }    //从充气布局视图
    鉴于=(的LinearLayout)inflater.inflate(R.layout.offer_tabs,集装箱,FALSE);    //设置的TabHost
    mTabHost =(FragmentTabHost)view.findViewById(android.R.id.tabhost);
    mTabHost.setup(getActivity(),getChildFragmentManager(),R.id.offer_realtabcontent);    //对于添加标签分割图像
    。mTabHost.getTabWidget()setDividerDrawable(R.drawable.tab_divider);    //对于各自的标签设置制表符
    mTabHost.addTab(mTabHost.newTabSpec(信息)。setIndicator(createTabView
            (mTabHost.getContext(),相关信息)),DetailedOfferFragment.class,NULL);
    mTabHost.addTab(mTabHost.newTabSpec(位置)。setIndicator(createTabView
            (mTabHost.getContext(),位置)),LocationFragment.class,NULL);    setRetainInstance(真);
    返回视图。
}/ *****自定义的TextView的tabs方法***** /
私有静态视图createTabView(最终上下文的背景下,最终字符串文本){
    查看查看= LayoutInflater.from(上下文).inflate(R.layout.tabs_text_bg,NULL);
    TextView的电视=(TextView中)view.findViewById(R.id.tabsText);
    tv.setText(文本);
    返回视图。
}
}

有关使用选项卡offer_tabs.xml布局

 &LT;?XML版本=1.0编码=UTF-8&GT?;
&LT; LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:背景=#6D6C6C
机器人:方向=垂直&GT;&LT; android.support.v4.app.FragmentTabHost
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@机器人:ID / tabhost
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent&GT;    &LT;的LinearLayout
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:方向=垂直&GT;        &LT; TabWidget
            机器人:ID =@机器人:ID /标签
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_weight =0
            机器人:方向=横向/&GT;        &LT;查看
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =2DP
            机器人:背景=#696969/&GT;        &LT;的FrameLayout
            机器人:ID =@机器人:ID / tabcontent
            机器人:layout_width =0dp
            机器人:layout_height =0dp
            机器人:layout_weight =0/&GT;        &LT;的FrameLayout
            机器人:ID =@ + ID / offer_realtabcontent
            机器人:layout_width =match_parent
            机器人:layout_height =0dp
            机器人:layout_weight =1/&GT;
    &LT; / LinearLayout中&GT;
&LT; /android.support.v4.app.FragmentTabHost>
&LT; / LinearLayout中&GT;

I would like to create tab in android but really I don't know how to create and
just downloaded some video but really this isn't helpful I used TabActivity unfortunately it's deprecated :( so that I need you to help me out thanks for any suggestion

解决方案

You must setup a FragmentActivity main class to run the Fragments.

You need to use the FragmentTabHost class available in the support library android.support.v4.app

The class for using 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;
}
}

The layout for using the tabs 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创建标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆