Android学習ノート4:ツールバーボタンの作成


前にActivityにボタンなどのコンポーネントを追加することができます.携帯電話の画面が小さいため、ツールバーを使ったり、ドロップダウンメニューなどでスペースを節約したりすることが多い.
Androidはツールバーボタンの強力なサポートを提供しています.
ツールバーボタンを追加MainActivityに検索ボタンを追加します.ボタンを1つ追加するにはこの3つのことが必要です
一、res/menu/activity_main.xmlにボタンの構成を追加する.このファイルは自動生成されたプロジェクトにすでに存在し、自動的に設定ボタンが作成されています.検索ボタンを1つ追加すればいいです.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">

    <item android:id="@+id/action_search"
        android:icon="@drawable/ic_search_white_24dp"
        android:title="@string/action_search"
        android:showAsAction="ifRoom" />
    <!-- Settings, should always be in the overflow -->
    <item android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:showAsAction="never" />
</menu>
drawablestringの2つのリソースを使用しているので、自分で作成する必要があります.
二、MainActivityに配置されたボタンを引用するMainActivityでは、onCreateOptionsMenuメソッドが宣言されていますが、変更する必要はありません.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

三、ボタンにイベントをバインドする
ここではonOptionsItemSelectedの方法もありますが、その中でイベントの傍受を増やすだけでいいです.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    switch (id) {
        case R.id.action_search: Toast.makeText(this, "      ", Toast.LENGTH_SHORT).show();
        case R.id.action_settings: Toast.makeText(this, "      ", Toast.LENGTH_SHORT).show();
    }

    return super.onOptionsItemSelected(item);
}

戻るボタンを追加
多くの場合、MainActivityではないアクティビティに戻るボタンを追加して、ユーザーが戻るボタンで前のアクティビティに戻ることができるようにする必要があります.アンドロイドは戻るボタンのデフォルトのサポートを提供しており、戻る論理コードを書く必要はありません.
戻るボタンの機能は2ステップで実現できます.ここではProfileActivityに戻りボタンを追加し、MainActivityに戻ります.
一、AndroidManifest.xmlのActivityの声明に親活動の声明を加える.
  <activity
        android:name=".ProfileActivity"
        android:label="@string/title_activity_profile"
        android:parentActivityName=".MainActivity">
        <!-- Parent activity meta-data to support 4.0 and lower -->
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity" />

        <intent-filter>
            <action android:name="com.lihongxun.Profile" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

4.0以下のデバイスをサポートするにはmeta-data行を追加する必要があります.そうしないと、android:parentActivityName=".MainActivity”を追加するだけです.
2.ProfileActivityで戻るボタンを有効にします.onCreate関数にコードを1行追加すればいいです.
 getSupportActionBar().setDisplayHomeAsUpEnabled(true);

そしてまた実行してみます.
3つのオーバーライドツールバー
デフォルトでは、上部のツールバーはスペースを占有しており、コンテンツ領域を下に押し出します.特に非表示ツールバーを動的に表示したい場合は、コンテンツ領域に上書きしたい場合があります.styles.xmlで上書きツールバーの使用を宣言するだけでよい
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="android:windowActionBarOverlay">true</item>
    <!-- Support library compatibility -->
    <item name="windowActionBarOverlay">true</item>
</style>

中間の2行itemは、宣言上書きツールバーであり、2行目が互換ライブラリである場合に使用されることに注意してください.