LoginSignup
14
14

More than 5 years have passed since last update.

New v7 appcompat library で対応したActionBarを試してみた

Last updated at Posted at 2013-07-26

Android Support Library, revision 18 でついにActionBarがサポートされたようなので、試してみました。
これで、ActionBarSherlockがお役御免になる???

以下に記載があります。
http://developer.android.com/tools/support-library/index.html

New v7 appcompat library:
Added ActionBar to allow implementation of the action bar user interface design pattern
back to Android 2.1 (API level 7) and higher.
Use of this class requires that you implement your activity by extending the new ActionBarActivity class.

以下、簡単な対応方法です。

  • 1.SDKを更新していない場合は、SDKを更新します。(現時点の最新は22.0.4です。)

  • 2./Applications/android-sdk-macosx/extras/android/support/v7/appcompatを
    プロジェクトのライブラリでとして取り込みます。

  • 3.appcompat/libs/android-support-v7-appcompat.jarをプロジェクトのlibsディレクトリにコピーしてビルドパスに追加します。

  • 4.Activityの継承元をActionBarActivityに変更します

java
public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
  • 5. values/style.xmlのparentを「Theme.AppCompat.Light.DarkActionBar」に変更します
xml
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

試しにActionBarSherlockからsupport libraryへの移行してみましたが、さほど手間はかかりませんでした。
ほぼ、SherlockActivity(SherlockFragmentActivity)を継承しているクラスを
ActionBarActivity(FragmentActivity)に変更するだけで済みました。(あとstyle.xmlも)

Navigation Drawerと組み合わせたサンプルを作成しましたので、参考にしてください。
ActionBarWithSupportLibrary

14
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
14