2012-05-03 5 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> 

이 엔터 종료 애니메이션

가 입력되어

<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

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

toast_frame_holo.9은 배경으로 사용되는 자원의 이름입니다. SDK를 검색하여 모든 크기를 찾습니다. 나는 보았다 곳

And here is the full source for Toasts

당신이 그 중 하나에 대해 잘 모르는 경우 모든 리소스에 대한 SDK를 검색, 즉이다.

+0

감사합니다 ...이 코드에 관심이있을 수 있습니다, 이것은 내 질문에 대답 것으로 보인다. 이것은 플랫폼에 관계없이 토스트보기를 여전히 동일하게 만들지 만, 나는 그것을 피할 수 없다고 생각합니다. –

0

내 ICS 장치에서 최근에 연결이 끊어져서 메시지를 보내지 못했습니다. 그것은 축배와 같이 보였지만 다른 글꼴 크기를 가지고 있었고, 2 줄이 길고, 빨간 테두리를 가지고있었습니다. 기본적으로 메시지를 보내지 못했습니다 (또는 연결 오류).

그게 당신이 찾고있는 것과 유사할까요?

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