将图片添加到面包?将图片添加到面包?(Adding image to Toast?)

2019-05-13 09:36发布

是否有可能以编程方式将图像添加到举杯弹出?

Answer 1:

是的 ,你可以使用的setView()方法,使用这种方法,你可以自定义吐司按照您的要求添加的ImageView或任何视图到Toast通知。

在这里,我已经创建被充气到吐司通知自定义布局文件,然后我已经使用的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();


Answer 2:

简单地说,使用以下:

Toast toast = new Toast(myContext);
ImageView view = new ImageView(myContext); 
view.setImageResource(R.drawable.image_icon); 
toast.setView(view); 
toast.show();


Answer 3:

您可以通过编程创建任何视图(因为我假设你是问如何做到这一点不使用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();


Answer 4:

Knickedi的解决方案是好的,但如果你只需要在文本旁边的图标,你可以利用的事实吐司有相同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));


Answer 5:

总有创建一个自定义布局的可能性。 有一个事实,我不喜欢有关:它打破了系统默认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;
}


Answer 6:

我觉得这是更好的,我们表明,我们传递给makeImageToast功能在图像上敬酒的文字...所以我遮挡Knickedi码和:

public class utility  {

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;
                ((TextView) child).setGravity(Gravity.CENTER);

            }
        }
    }

    // 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;

    // 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;

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


    // modify root layout
    linearLayout.setOrientation(LinearLayout.HORIZONTAL);
    linearLayout.setBackgroundResource(imageResId);
    linearLayout.setGravity(Gravity.CENTER);
    linearLayout.setHorizontalGravity(Gravity.CENTER);
    linearLayout.setHorizontalGravity(Gravity.CENTER);
    //addView(imageView, 0);

    return toast;
}

}

这就是使用它:

utility.makeImageToast(getApplicationContext(),
                 R.drawable.your_image,"your_text",Toast.LENGTH_LONG).show();


Answer 7:

Toast aa = Toast.makeText(getBaseContext(), "OPEN",Toast.LENGTH_SHORT);
ImageView cc = new ImageView(getBaseContext());
cc.setImageResource(R.drawable.a);
aa.setView(cc);
aa.show();


文章来源: Adding image to Toast?