タイトルバーにボタンを表示する |
ここではタイトルバーにボタンを表示にする方法を確認していきます。
タイトルバーにボタンを付けるなどのカスタマイズをする場合には、アクティビティの「requestWindowFeature」メソッドにWindow.FEATURE_CUSTOM_TITLEを設定します。 getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar); でタイトルバーのレイアウトを指定しています。 Titlebar2Activity.java package goodroid.sample.titlebar2; import android.app.Activity; import android.os.Bundle; import android.view.Window; public class Titlebar2Activity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // タイトルバーのカスタマイズを設定可能にする requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.main); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar); } } /res/layoutの配下に、タイトルバーのレイアウトファイルを作成します。 titlebar.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ボタン" /> </LinearLayout> このままですと、タイトルバーの高さが1行分しかないためボタンの表示が下の実行結果のように潰れてしまいます。 実行結果 ![]() そこで、タイトルバーの高さを調節するために、/res/values の配下にテーマファイルを作成します。 themes.xml <?xml version="1.0" encoding="utf-8"?> <resources> <style name="MyTheme" parent="android:Theme"> <item name="android:windowTitleSize">40dip</item> <item name="android:windowFullscreen">false</item> </style> </resources> 続いて、AndroidManifest.xmlの「Activity」タグに今作ったテーマを追加します。 (4行目が該当します) AndroidManifest.xml <activity android:name=".Titlebar2Activity" android:label="@string/app_name" android:theme="@style/MyTheme"> </activity> どうでしょうか?文字やボタンのサイズを調節してちょうどいい高さに変更してください。 実行結果 ![]() 【関連記事】 ・タイトルバーを非表示にする |
13569 views | コメント:0 | 2012-06-08 |
コメント
|
|
まだこの記事にコメントはありません |