2012-10-20 27 views
11

Tôi đã recentrly thử một Toast tùy chỉnh sau khi hướng dẫn:Tạo một tùy chỉnh Toast fullscreen

http://developer.android.com/guide/topics/ui/notifiers/toasts.html#CustomToastView

với một diện như:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/toast_layout_root" 
       android:orientation="horizontal" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:padding="8dp" 
       android:background="#DAAA" 
       > 
    <ImageView android:src="@drawable/droid" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="8dp" 
       /> 
    <TextView android:id="@+id/text" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textColor="#FFF" 
       /> 
</LinearLayout> 

Nhưng dường như fill_parent trong cách bố trí gốc không có hiệu lực.

Bạn có ý tưởng về cách tôi có thể khắc phục điều này để nhận được bánh mì nướng toàn màn hình không?

Trả lời

19

Thêm này trước khi bạn hiển thị Toast:

toast.setGravity(Gravity.FILL, 0, 0); 
1

Để hoàn toàn điền vào một bánh mì nướng ngang và dọc trong kích thước của container của nó bạn cần phải sử dụng

Gravity.FILL như đã đề cập trong câu trả lời của Yoah.

Tôi đã thử làm theo và nó đã hoạt động.

Toast toast = new Toast(getApplicationContext()); 
toast.setGravity(Gravity.FILL, 0, 0); 
toast.setView(view); //inflated view 
toast.setDuration(Toast.LENGTH_LONG); 
toast.show();