2012年2月2日木曜日

Toast (表示位置変更)

Toastは、普通は画面の下のほうにメッセージを表示します。


今回はこの表示位置を変えてみましょう。


Toast表示位置変更

Toastの表示位置を変更するにはsetGravity()メソッドを使います。
void android.widget.Toast.setGravity(int gravity, int xOffset, int yOffset);
gravityにはGravityクラスの定数、TOP/BOTTOM/LEFT/RIGHT等を指定します。例えば「左上」に表示したい場合はTOPとLEFTのOR値を指定します。xOffsetとyOffsetは、X座標とY座標のオフセットです。

以下のサンプルではクリックしたボタンによって表示位置を変えています。四隅に表示する際のY座標だけオフセットを指定してみました。

Androidプロジェクトの設定
プロジェクト名:ToastTest2a
ビルドターゲット:Android 2.1-update1
アプリケーション名:ToastTest2a
パッケージ名:jp.co.triware.samples.ToastTest2a
アクティビティーの作成:ToastTest2aActivity
最小SDKバージョン:7
ToastTest2aActivity.java
package jp.co.triware.samples.ToastTest2a;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class ToastTest2aActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final int[][] buttons = {
            {   R.id.top_left_btn,      Gravity.TOP | Gravity.LEFT,     0,  40  },
            {   R.id.top_btn,           Gravity.TOP,                    0,  0   },
            {   R.id.top_right_btn,     Gravity.TOP | Gravity.RIGHT,    0,  40  },
            {   R.id.left_btn,          Gravity.LEFT,                   0,  0   },
            {   R.id.center_btn,        Gravity.CENTER,                 0,  0   },
            {   R.id.right_btn,         Gravity.RIGHT,                  0,  0   },
            {   R.id.bottom_left_btn,   Gravity.BOTTOM | Gravity.LEFT,  0,  40  },
            {   R.id.bottom_btn,        Gravity.BOTTOM,                 0,  0   },
            {   R.id.bottom_right_btn,  Gravity.BOTTOM | Gravity.RIGHT, 0,  40  },
        };
        final Button btnNormal = (Button)findViewById(R.id.normal_btn);
        btnNormal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(), btnNormal.getText(), Toast.LENGTH_SHORT).show();
            }
        });
        for (int i = 0; i < buttons.length; i ++) {
            final Button btn = (Button)findViewById(buttons[i][0]);
            final int gravity = buttons[i][1];
            final int offsetX = buttons[i][2];
            final int offsetY = buttons[i][3];
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast toast = Toast.makeText(getApplicationContext(), btn.getText(), Toast.LENGTH_SHORT);
                    toast.setGravity(gravity, offsetX, offsetY);
                    toast.show();
                }
            });
        }
    }
}
ボタンのラベルを取得して、それを指定した位置に表示します。

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_vertical"
    >
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
        <Button
            android:id="@+id/top_left_btn"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="左上"
            />
        <Button
            android:id="@+id/top_btn"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="上"
            />
        <Button
            android:id="@+id/top_right_btn"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="右上"
            />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
        <Button
            android:id="@+id/left_btn"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="左"
            />
        <Button
            android:id="@+id/center_btn"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="中"
            />
        <Button
            android:id="@+id/right_btn"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="右"
            />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
        <Button
            android:id="@+id/bottom_left_btn"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="左下"
            />
        <Button
            android:id="@+id/bottom_btn"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="下"
            />
        <Button
            android:id="@+id/bottom_right_btn"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="右下"
            />
    </LinearLayout>
    <Button
        android:id="@+id/normal_btn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="通常のToast表示"
        />
</LinearLayout>
ここは新しいことはしていません。表示位置を変えたToastを表示するためのボタンと、通常のToast表示を行うためのボタンを並べています。

実行結果
ボタンクリック時に指定した場所にメッセージを表示します。左上・右上・左下・右下の分だけ、少し内側に表示しています。

0 件のコメント:

コメントを投稿