2011-09-27 42 views

Trả lời

80

, bạn có thể thêm ImageView hay bất cứ cái nhìn vào thông báo bánh mì nướng bằng cách sử dụng phương pháp setview(), sử dụng phương pháp này, bạn có thể tùy chỉnh Toast theo yêu cầu của bạn.

Ở đây tôi đã tạo tệp Bố cục tùy chỉnh để được thổi phồng vào thông báo Bánh mì nướng và sau đó tôi đã sử dụng bố cục này trong thông báo Bánh mì nướng bằng cách sử dụng phương thức 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(); 
+0

@Blundell [** Here **] (http://www.technotalkative.com/android-custom-toast- thông báo /) là hướng dẫn chi tiết với snap đầu ra. –

+0

@PareshMayani: :)))) .. Câu trả lời tuyệt vời Bhai .. !!! –

+0

@HareshChaudhary Cảm ơn :) –

14

Bạn có thể tạo bất kỳ chế độ xem nào theo chương trình (vì tôi giả sử bạn đang yêu cầu cách thực hiện điều này KHÔNG sử dụng LayoutInflater) và gọi setView trên Toast bạn đã thực hiện.

//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(); 
1

Luôn có khả năng để tạo ra một bố cục tùy chỉnh. Có một sự kiện mà tôi không thích về điều đó: Nó phá vỡ giao diện người dùng mặc định của hệ thống. Điều này có thể khác nhau trên các nền tảng và triển khai khác nhau. Không có cách nào đơn giản để sử dụng tài nguyên mặc định của hệ thống vì vậy tôi đã quyết định để hack các bánh mì nướng và buộc một hình ảnh vào nó.

Gợi ý: Bạn có thể lấy tài nguyên mặc định như thế này:
Toast.makeToast(context, "", 0).getView().getBackground()


Dưới đây là một helper mà sẽ hiển thị một hình ảnh ở phía trước của thông điệp chúc mừng: Helper.makeImageToast(context, R.drawable.my_image, "Toast with image", Toast.LENGTH_SHORT).show()

tôi sử dụng để cho biết thành công, thông tin hoặc lỗi. Làm cho một bánh mì nướng thông tin đẹp hơn và ấn tượng hơn ...

(Đó là đáng nói đến là căn cứ Hack trên thực tế là bánh mì nướng nội bộ đang sử dụng một LinearLayout nên không phải là hệ thống và thực hiện độc lập. Xem ý kiến.)

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; 
} 
+0

Điều này chỉ hoạt động nếu bố cục của Bánh mì nướng là loại 'LinearLayout'. Không có liên hệ nào từ trước rằng Toast sẽ luôn có LinearLayout. Mã của bạn đề cập đến điều này bằng cách không thêm hình ảnh nhưng đáng chú ý là giải pháp này không phải là thiết bị/phiên bản độc lập. – Graeme

+0

@Graeme Bạn nói đúng. Cảm ơn bạn đã gợi ý. Cách tốt hơn là tạo lại bánh mì nướng trong LinearLayout của riêng mình. Tôi sẽ cập nhật câu trả lời của tôi khi tôi tìm thấy một số thời gian cho điều đó. – Knickedi

20

đơn giản, sử dụng như sau:

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

giải pháp Knickedi là tốt, nhưng nếu bạn chỉ cần một biểu tượng bên cạnh các văn bản mà bạn có thể tận dụng thực tế là Toast có một TextView được xác định trước với cùng một ID và đặt biểu tượng trên 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)); 
0

Tôi nghĩ điều này tốt hơn là chúng tôi hiển thị văn bản của bánh mì nướng trên hình ảnh mà chúng tôi chuyển đến hàm makeImageToast ... vì vậy tôi che mã Knickedi và:

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

}

và điều này là sử dụng nó:

utility.makeImageToast(getApplicationContext(), 
       R.drawable.your_image,"your_text",Toast.LENGTH_LONG).show(); 
Các vấn đề liên quan