62

トーストポップアップにプログラムで画像を追加することは可能ですか?

4

8 に答える 8

87

はい、 setView() メソッドを使用してイメージビューまたは任意のビューをトースト通知に追加できます。このメソッドを使用して、要件に従ってトーストをカスタマイズできます。

ここでは、トースト通知に膨らませるカスタム レイアウト ファイルを作成し、setView() メソッドを使用してトースト通知でこのレイアウトを使用しました。

cust_toast_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/relativeLayout1"
  android:background="@android:color/white">

    <TextView
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/textView1" android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:text="PM is here"
        android:gravity="center"
        android:textColor="@android:color/black">
    </TextView>

    <ImageView
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:src="@drawable/new_logo"
        android:layout_below="@+id/textView1"
        android:layout_margin="5dip"
        android:id="@+id/imageView1">
    </ImageView>

    <TextView
        android:id="@+id/textView2"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:text="This is the demo of Custom Toast Notification"
        android:gravity="center"
        android:layout_below="@+id/imageView1"
        android:textColor="@android:color/black">
    </TextView>

</RelativeLayout>

CustomToastDemoActivity.java

LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.cust_toast_layout, 
    (ViewGroup)findViewById(R.id.relativeLayout1));

Toast toast = new Toast(this);
toast.setView(view);
toast.show();
于 2011-09-28T05:41:55.017 に答える
25

簡単に、次を使用します。

Toast toast = new Toast(myContext);
ImageView view = new ImageView(myContext); 
view.setImageResource(R.drawable.image_icon); 
toast.setView(view); 
toast.show();
于 2013-04-20T00:20:54.683 に答える
14

Knickedi のソリューションは優れていますが、テキストの横にアイコンのみが必要な場合は、Toast に同じ ID を持つ事前定義された TextView があるという事実を利用して、TextView にアイコンを設定できます。

Toast toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
TextView tv = (TextView) toast.getView().findViewById(android.R.id.message);
if (null!=tv) {
    tv.setCompoundDrawablesWithIntrinsicBounds(icon, 0, 0, 0);
    tv.setCompoundDrawablePadding(context.getResources().getDimensionPixelSize(R.dimen.padding_toast));
于 2013-11-08T12:49:43.217 に答える
14

プログラムで任意のビューを作成し (LayoutInflater を使用せずにこれを行う方法を尋ねていると想定しているため)、作成したトーストで setView を呼び出すことができます。

    //Create a view here
    LinearLayout v = new LinearLayout(this);
    //populate layout with your image and text or whatever you want to put in here

    Toast toast = new Toast(getApplicationContext());
    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
    toast.setDuration(Toast.LENGTH_LONG);
    toast.setView(v);
    toast.show();
于 2011-09-27T15:55:35.797 に答える
1

カスタム レイアウトを作成する可能性は常にあります。私が気に入らなかった点が 1 つあります。それは、システムのデフォルトのトースト UI を壊してしまうことです。これは、プラットフォームや実装によって異なる場合があります。システムの既定のリソースを使用する簡単な方法がないため、トーストをハックして画像を強制することにしました。

ヒント: デフォルトのリソースは次のように取得できます。
Toast.makeToast(context, "", 0).getView().getBackground()


トースト メッセージの前に画像を表示するヘルパーを次に示します。 Helper.makeImageToast(context, R.drawable.my_image, "Toast with image", Toast.LENGTH_SHORT).show()

これを使用して、成功、情報、またはエラーを示します。トースト情報をより美しく、より表現力豊かに...

(ハックは、内部トーストが を使用しているという事実に基づいているLinearLayoutため、システムと実装に依存しないことに言及する価値があります。コメントを参照してください。)

public static Toast makeImageToast(Context context, int imageResId, CharSequence text, int length) {
    Toast toast = Toast.makeText(context, text, length);

    View rootView = toast.getView();
    LinearLayout linearLayout = null;
    View messageTextView = null;

    // check (expected) toast layout
    if (rootView instanceof LinearLayout) {
        linearLayout = (LinearLayout) rootView;

        if (linearLayout.getChildCount() == 1) {
            View child = linearLayout.getChildAt(0);

            if (child instanceof TextView) {
                messageTextView = (TextView) child;
            }
        }
    }

    // cancel modification because toast layout is not what we expected
    if (linearLayout == null || messageTextView == null) {
        return toast;
    }

    ViewGroup.LayoutParams textParams = messageTextView.getLayoutParams();
    ((LinearLayout.LayoutParams) textParams).gravity = Gravity.CENTER_VERTICAL;

    // convert dip dimension
    float density = context.getResources().getDisplayMetrics().density;
    int imageSize = (int) (density * 25 + 0.5f);
    int imageMargin = (int) (density * 15 + 0.5f);

    // setup image view layout parameters
    LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams(imageSize, imageSize);
    imageParams.setMargins(0, 0, imageMargin, 0);
    imageParams.gravity = Gravity.CENTER_VERTICAL;

    // setup image view
    ImageView imageView = new ImageView(context);
    imageView.setImageResource(imageResId);
    imageView.setLayoutParams(imageParams);

    // modify root layout
    linearLayout.setOrientation(LinearLayout.HORIZONTAL);
    linearLayout.addView(imageView, 0);

    return toast;
}
于 2011-10-24T22:19:26.717 に答える