android – FragmentActivty中的ActionBarCompat

前端之家收集整理的这篇文章主要介绍了android – FragmentActivty中的ActionBarCompat前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我收到了错误
“对于MainActivity类型,方法getSupportActionBar()未定义”

import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.Tab;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {

AppSectionsPagerAdapter mAppSectionsPagerAdapter;

ViewPager mViewPager;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());


    final ActionBar actionBar = getSupportActionBar();


    //actionBar.setHomeButtonEnabled(false);


    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);


    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mAppSectionsPagerAdapter);
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {

            actionBar.setSelectedNavigationItem(position);
        }
    });

    for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
        actionBar.addTab(
                actionBar.newTab()
                        .setText(mAppSectionsPagerAdapter.getPageTitle(i))
                        .setTabListener(this));
    }
}
.
.
.
}

请帮帮我. setHomeButtonEnabled()是否支持

哪个更好ActionBarCompat或ActionBarSherlock?

最佳答案
当应用程序使用getSupportActionBar()时,您的活动必须从ActionBarActivity扩展.它在您已添加支持包中定义,我可以在您的导入中看到.

public class MainActivity extends ActionBarActivity
                          implements ActionBar.TabListener {
    // your code goes here
}
原文链接:https://www.f2er.com/android/430306.html

猜你在找的Android相关文章