2012-05-03 43 views
2

我知道我可以使用自定義的吐司佈局輕鬆完成所有這些工作,但是在創建自定義佈局時,我不再使用系統的默認吐司視圖。有沒有辦法改變吐司的顏色和文字大小/顏色,但保留系統吐司佈局?

例如:敬酒當然看起來不同在Android 2.2與Android 4.0。如果我爲我的烤麪包創建自定義視圖,那麼它在兩個版本中看起來都完全一樣,但我想要的是保留其「... Androidiness」,因爲缺少更好的單詞。基本上,有沒有人知道默認的吐司XML佈局?這甚至有可能嗎?

回答

6

這是Toasts使用的默認佈局。

transient_notification

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:background="@drawable/toast_frame"> 

<TextView 
    android:id="@android:id/message" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:textAppearance="@style/TextAppearance.Small" 
    android:textColor="@color/bright_foreground_dark" 
    android:shadowColor="#BB000000" 
    android:shadowRadius="2.75"/> 
</LinearLayout> 

這些

的進入和退出動畫ENTER

<alpha xmlns:android="http://schemas.android.com/apk/res/android" 
     android:interpolator="@interpolator/decelerate_quad" 
     android:fromAlpha="0.0" android:toAlpha="1.0" 
     android:duration="@android:integer/config_longAnimTime" /> 

EXIT

​​

toast_frame_holo.9是用於後臺資源的名稱。搜索SDK以查找所有大小。

And here is the full source for Toasts

如果你不確定任何的是,搜索所有資源的SDK,我就是在那裏看着。

+0

謝謝,這似乎回答我的問題。儘管如此,這仍然會讓敬酒看起來一樣,但我想我不能避免這種情況。 –

0

在我的ICS設備上,由於丟失連接,我最近發送了一條消息失敗。它看起來像一個烤麪包,但有不同的字體大小,兩行長,並有一個紅色的邊框。它基本上表示該消息未能發送(或連接錯誤)。

這可能類似於你要找的東西?

0

我想保留默認烤麪包,但找到了解決方案,它告訴我如何爲所有系統創建單個烤麪包佈局。所以我創建了這個幫手。我知道,這是一種黑客攻擊,但它對我來說工作正常。

在這裏,我添加了一個圖像的文本前面的麪包,而不觸及其原生外觀。您可以在這裏輕鬆修改文字的顏色和大小。

也許有人可能有興趣在這個代碼片段...

private static Toast makeImageToast(int imageResId, CharSequence text, int duration) { 
    Toast toast = Toast.makeText(mContext, text, duration); 

    View rootView = toast.getView(); 
    LinearLayout linearLayout = null; 
    TextView 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) { 
     // failed to create image toast layout, using usual toast 
     return toast; 
    } 

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

    // convert dip dimension 
    int imageSize = dipToPixel(25); 
    int imageMargin = dipToPixel(15); 

    // 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(mContext); 
    imageView.setId(TOAST_IMAGE_ID); 
    imageView.setImageResource(imageResId); 
    imageView.setLayoutParams(imageParams); 

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

    return toast; 
} 

public static int dipToPixel(float dip) { 
    return (int) (dip * mContext.getResources().getDisplayMetrics().density + 0.5f); 
}