如何在滑动视图中添加操作栏?

时间:2014-10-21 19:49:33

标签: android android-actionbar swipe

我一直在关注http://developer.android.com/training/implementing-navigation/lateral.html以创建包含片段的滑动视图。

特别是,我采用了代码块

public class CollectionDemoActivity extends FragmentActivity {
    // When requested, this adapter returns a DemoObjectFragment,
    // representing an object in the collection.
    DemoCollectionPagerAdapter mDemoCollectionPagerAdapter;
    ViewPager mViewPager;

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

        // ViewPager and its adapters use support library
        // fragments, so use getSupportFragmentManager.
        mDemoCollectionPagerAdapter =
                new DemoCollectionPagerAdapter(
                        getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mDemoCollectionPagerAdapter);
    }
}

// Since this is an object collection, use a FragmentStatePagerAdapter,
// and NOT a FragmentPagerAdapter.
public class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter {
    public DemoCollectionPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        Fragment fragment = new DemoObjectFragment();
        Bundle args = new Bundle();
        // Our object is just an integer :-P
        args.putInt(DemoObjectFragment.ARG_OBJECT, i + 1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getCount() {
        return 100;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return "OBJECT " + (position + 1);
    }
}

// Instances of this class are fragments representing a single
// object in our collection.
public static class DemoObjectFragment extends Fragment {
    public static final String ARG_OBJECT = "object";

    @Override
    public View onCreateView(LayoutInflater inflater,
            ViewGroup container, Bundle savedInstanceState) {
        // The last two arguments ensure LayoutParams are inflated
        // properly.
        View rootView = inflater.inflate(
                R.layout.fragment_collection_object, container, false);
        Bundle args = getArguments();
        ((TextView) rootView.findViewById(android.R.id.text1)).setText(
                Integer.toString(args.getInt(ARG_OBJECT)));
        return rootView;
    }
}

并将其扩展以满足我的需求。

如何修改此选项,以便屏幕顶部有一个操作栏,只有活动名称(位于中心),应用程序图标和箭头返回父活动(左侧) )?

我尝试过添加

final ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(true);

上面代码中的CollectionDemoActivity onCreate,但这会导致我的应用程序崩溃。

编辑:

我的styles.xml包含

<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
<style name="AppTheme" parent="AppBaseTheme">

我的清单包括

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

...在应用程序中包含活动标签。没有任何活动覆盖应用程序级别的android:theme集。父活动有一个操作栏,但是孩子(我在这篇文章中询问的滑动活动)没有。

1 个答案:

答案 0 :(得分:2)

您的问题本质上非常简单,因为它与滑动视图几乎没有关系:您只需要在FragmentActivity中使用ActionBar。

The documentation makes a special note about using the ActionBar in a FragmentActivity

  

注意:如果要实现包含操作栏的活动,您应该使用ActionBarActivity类,它是此类的子类,因此允许您在API上使用Fragment API 7级及以上。

让您的活动扩展ActionBarActivity,并使用getSupportActionBar()获取对ActionBar的引用。

相关问题