0

如果我手动触发事件,我有一个支持操作栏可以正常工作。但是,如果我将它留给 tabhost 来调用它,那么从 getSupportActionbar() 返回的操作栏为空。

我在另一个问题中在 Stack 上听到了这一点,但没有人提供和回答。(显然它只发生在 Android 3 及更高版本上)。有没有人有任何想法?

我的标签主持人:

public class NavTab extends TabActivity {

    TabHost tabHost;

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

        Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

        // Initialize a TabSpec for each tab and add it to the TabHost
        intent = new Intent().setClass(this, SummaryPage.class);
        spec = tabHost.newTabSpec("Summary");
        spec.setIndicator("Account", getResources().getDrawable(R.drawable.tab_icon_summary));
        spec.setContent(intent);
        tabHost.addTab(spec);

        //Feedback
        intent = new Intent().setClass(this, FeedbackPage.class);
        spec = tabHost.newTabSpec("Feedback");
        spec.setIndicator("Feedback", getResources().getDrawable(R.drawable.tab_icon_summary));
        spec.setContent(intent);
        tabHost.addTab(spec);

        //Payment Locations
        intent = new Intent().setClass(this, PaymentLocationsActivity.class);
        spec = tabHost.newTabSpec("Payment Locations");
        spec.setIndicator("Pay Loc", getResources().getDrawable(R.drawable.tab_icon_summary));
        spec.setContent(intent);
        tabHost.addTab(spec);

        //Usage Alert
        intent = new Intent().setClass(this, UsageAlertPage.class);
        spec = tabHost.newTabSpec("Usage Alerts");
        spec.setIndicator("Alerts", getResources().getDrawable(R.drawable.tab_icon_summary));
        spec.setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(1);

    }


}

我的活动

public class PageWithActionBar extends SherlockActivity implements ActionBar.OnNavigationListener {

    private static String TAG = "mymeter-Main";
    private List<Account> accounts = new LinkedList<Account>();
    private LocationAdapter locationAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);


        ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
        actionBar.setDisplayShowTitleEnabled(false);

        accounts.add(new Account("123456789", "4-15 Rose Rd", "Auckland 1021"));
        accounts.add(new Account("0987654321", "49 Ronaki Rd", "Auckland 1043"));
        locationAdapter = new LocationAdapter(this, accounts);
        actionBar.setListNavigationCallbacks(locationAdapter, this);


    }
}
4

1 回答 1

2

您的 TabActivity 需要扩展SherlockActivity,请查看 Tabs 上的 ActionBarSherlock 示例

于 2012-11-19T04:31:30.957 回答