android - 如何将操作栏添加到滑动 View ?

标签 android android-actionbar swipe

我一直在关注 http://developer.android.com/training/implementing-navigation/lateral.html使用 fragment 创建滑动 View 。

特别是,我已经获取了代码块

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;
    }
}

并扩展它以满足我的需要。

我该如何修改它,以便在屏幕顶部有一个操作栏,其中只有 Activity 的名称(在中间)、应用程序图标和返回父 Activity 的箭头(在左边) )?

我试过添加

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

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

编辑:

我的 styles.xml 包括

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

我的 list 包括

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

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

最佳答案

您的问题本质上非常简单,因为它与滑动 View 无关:您只需要在 FragmentActivity 中使用 ActionBar。

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

Note: If you want to implement an activity that includes an action bar, you should instead use the ActionBarActivity class, which is a subclass of this one, so allows you to use Fragment APIs on API level 7 and higher.

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

关于android - 如何将操作栏添加到滑动 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26495084/

相关文章:

android - Andengine,找不到使用倾斜/加速度计的方法

android - 为什么要同时使用溢出菜单和硬件设置按钮菜单来显示相同​​的选项?

Android ActionBar 菜单 : Prevent onPressed Color

android - FadingActionBar 与 ActionBarCompat

java - Android - 图像缩放库

html - 牛 : How do we use the PageIndicator for Wearable Devices

android - 如何在内容缩小时更新自定义 WebView 高度请求

java - 我的 android 应用程序每 5 秒只打印一张票

android - 无法使用 Theme.AppCompat 设置十六进制颜色和查看操作栏上的图标

jquery - 使用 jQuery 检测 iPhone 滑动