是否可以通過編程將圖像添加到Toast彈出窗口?將圖像添加到吐司?
回答
是,您可以使用的setView()方法,使用這種方法,你可以自定義吐司按照您的要求添加的ImageView或任何視圖到Toast通知。
在這裏我創建了一個自定義佈局文件,將其充氣到Toast通知中,然後我通過使用setView()方法在Toast通知中使用了此佈局。
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();
@Blundell [** Here **](http://www.technotalkative.com/android-custom-toast-通知/)是帶輸出捕捉的詳細教程。 –
@PareshMayani::))))..好的答案Bhai .. !!! –
@HareshChaudhary謝謝:) –
您可以編程方式創建任何視圖(因爲我假設您問的是如何在不使用LayoutInflater的情況下執行此操作)並在您所做的Toast上調用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();
總有創建一個自定義佈局的可能性。有一個我不喜歡的事實:它打破了系統默認的Toast 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;
}
簡單地說,使用以下:
Toast toast = new Toast(myContext);
ImageView view = new ImageView(myContext);
view.setImageResource(R.drawable.image_icon);
toast.setView(view);
toast.show();
Knickedi的解決方案是好的,但如果你只需要在文本旁邊的圖標,你可以利用的事實吐司有一個預先定義的TextView與相同的ID並在TextV上設置圖標IEW:
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));
我覺得這是更好的,我們表明,我們傳遞給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();
- 1. 添加Materializecss吐司到SVG
- 2. 圖像動態地添加到自定義的吐司
- 3. 吐司與圖像背景
- 4. 將圖像吐司轉換成圖像AlertDialog
- 5. 如何將按鈕添加到Windows 8吐司通知?
- 6. Android像鞦韆吐司
- 7. 吐司
- 8. Creatinng定製吐司視圖
- 9. 如何添加吐司說「沒有互聯網連接」到webview?
- 10. 更改吐司到GridView
- 11. 吐司崩潰
- 12. 吐司消息
- 13. 錯誤吐司
- 14. 吐痰文字,將它添加到表
- 15. 將圖像添加到GUI
- 16. 將圖像添加到TextField
- 17. 將圖像添加到Tkinter
- 18. 將圖像添加到HighChart
- 19. 將圖像添加到jsfiddle
- 20. 將圖像添加到jframe
- 21. 將圖像添加到JSfiddle
- 22. 將圖像添加到GridView
- 23. 將圖像添加到JFrame
- 24. 將圖像添加到GUI
- 25. 將圖像添加到UITextView
- 26. 將圖像添加到asp:linkbutton?
- 27. 將圖像添加到JFrame
- 28. 將圖像添加到plist
- 29. 將圖像添加到JApplet
- 30. 將域添加到圖像
[嘗試看看這裏(http://developer.android.com/ guide/topics/ui/notifiers/toasts.html#CustomToastView) – crbin1
@SpK +1這樣的好問題 – swiftBoy