Android 使用 FragmentActivity 而不是 TabActivity

Android use FragmentActivity instead of TabActivity

我想将 TabActivity 更改为 FragmentActivity,因为 TabActivity 已被弃用。

我的密码是

public class Fragment_Activity extends TabActivity implements TabHost.OnTabChangeListener {

    /** Called when the activity is first created. */
    TabHost tabHost;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_tabs);

        // Get TabHost Refference
        tabHost = getTabHost();

        // Set TabChangeListener called when tab changed
        tabHost.setOnTabChangedListener(this);

        TabHost.TabSpec spec;
        Intent intent;

        /************* TAB1 ************/
        // Create  Intents to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, OccasionFragment.class);
        spec = tabHost.newTabSpec("First").setIndicator("")
                .setContent(intent);

        //Add intent to tab
        tabHost.addTab(spec);

        /************* TAB2 ************/
        intent = new Intent().setClass(this, InvitationFragment.class);
        spec = tabHost.newTabSpec("Second").setIndicator("")
                .setContent(intent);
        tabHost.addTab(spec);

        /************* TAB3 ************/
        intent = new Intent().setClass(this, FriendsFragment.class);
        spec = tabHost.newTabSpec("Third").setIndicator("")
                .setContent(intent);
        tabHost.addTab(spec);

        // Set drawable images to tab
        tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.ic_action_event);
        tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.ic_action_phone);

        // Set Tab1 as Default tab and change image
        tabHost.getTabWidget().setCurrentTab(0);
        tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.ic_action_person);


    }

    @Override
    public void onTabChanged(String tabId) {

        /************ Called when tab changed *************/

        //********* Check current selected tab and change according images *******/

        for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
        {
            if(i==0)
                tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.ic_action_event);
            else if(i==1)
                tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.ic_action_phone);
            else if(i==2)
                tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.ic_action_person);
        }


        if(tabHost.getCurrentTab()==0)
            tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.ic_action_event);
        else if(tabHost.getCurrentTab()==1)
            tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.ic_action_phone);
        else if(tabHost.getCurrentTab()==2)
            tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.ic_action_person);

    }`

它的工作,但我想要改变FragmentActivity

Android 开发者 link : TabActivity

当我使用 FragmentActivity 时无法正常工作 请帮助我。

替换

 tabHost = getTabHost();

tabHost = (TabHost)findViewById(android.R.id.tabhost);
tabHost.setup();

这将适用于 FragmentActivity,假设所有其他事情都是正确的。

fragment_tabs.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <include layout="@layout/header_layout" />

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1" />

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="-4dp"
        android:layout_weight="0" />
</LinearLayout>

fragment_activity.java

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;

public class Fragment_Activity extends FragmentActivity {

/** Called when the activity is first created. */
private static final String TAB_1_TAG = "Invitation";
private static final String TAB_2_TAG = "Occasion";
private static final String TAB_3_TAG = "Friends";
private FragmentTabHost tabHost;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_tabs);

    // Get TabHost Refference
    tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    tabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);

    tabHost.addTab(tabHost.newTabSpec(TAB_1_TAG).setIndicator(""), InvitationFragment.class, null);
    tabHost.addTab(tabHost.newTabSpec(TAB_2_TAG).setIndicator(""), OccasionFragment.class, null);
    tabHost.addTab(tabHost.newTabSpec(TAB_3_TAG).setIndicator(""), ContactActivity.class, null);

    // Set drawable images to tab
    tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.ic_action_event);
    tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.ic_action_phone);

    // Set Tab1 as Default tab and change image
    tabHost.getTabWidget().setCurrentTab(0);
    tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.ic_action_person);

}

}

invitation_tab.xml

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class InvitationFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View V = inflater.inflate(R.layout.invitation_tab, container, false);

    return V;
}

}

终于解决了 tab_activity 问题。 感谢您帮助@ZygoteInit..