アンドロイドのあれこれ
[PR]
×
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
Begin with ActionBar
去年のひとりAndroid Advent CalendarではAndroidアプリのデフォルトタイトルバーの代わりにレイアウトでタイトルを作るような記事を書きましたが、API level 11から追加されたActionBarならそんな必要はありません(端末のOSがAndroid 3.x以上)
Action Barについて
http://developer.android.com/guide/topics/ui/actionbar.html
今では普通にEclipse(ADTインストール済)から作成されたAndroidのプロジェクトはAction Barがすでに対応されています。
Action Barにアイコンやメニューアイテムなどを追加することもできます。
Action Barについて
http://developer.android.com/guide/topics/ui/actionbar.html
今では普通にEclipse(ADTインストール済)から作成されたAndroidのプロジェクトはAction Barがすでに対応されています。
Action Barにアイコンやメニューアイテムなどを追加することもできます。
・AndroidManifest.xmlにThemeを指定して対応する

themeにTheme.Holoを指定します。またはバーション分けてstyle指定する書き方もあります
AndroidManifest.xml
res/values-v11/styles.xml

・ActionBarを非表示に
リソースで指定する方法
Activeのソースで指定する方法
・ActionBarにメニューを追加
MainActivity.java
res/menu/activity_main.xml

showAsActionにifRoomで指定してアイコン表示する

showAsAction="ifRoom|withText"でアイコンとテキストを表示させる

メニューのリソースについてはここで参考できます。
http://developer.android.com/guide/topics/resources/menu-resource.html
....
<uses-sdk
android:targetSdkVersion="11" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/android:Theme.Holo" >
....
themeにTheme.Holoを指定します。またはバーション分けてstyle指定する書き方もあります
AndroidManifest.xml
....
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
res/values-v11/styles.xml
<resources>
<style name="AppThem" parent="android:Theme.Holo.Light">
</style>
</resources>
・ActionBarを非表示に
リソースで指定する方法
...
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Holo.NoActionBar" >
...
Activeのソースで指定する方法
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
actionBar.hide();
}
...
・ActionBarにメニューを追加
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main, menu);
return true;
}
}
res/menu/activity_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/menu_settings"
android:title="@string/menu_settings"
android:showAsAction="never"
/>
</menu>
showAsActionにifRoomで指定してアイコン表示する
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/menu_settings"
android:icon="@drawable/ic_menu_info_details"
android:title="@string/menu_settings"
android:showAsAction="ifRoom"
/>
</menu>
showAsAction="ifRoom|withText"でアイコンとテキストを表示させる
メニューのリソースについてはここで参考できます。
http://developer.android.com/guide/topics/resources/menu-resource.html
COMMENT