2016-09-16 5 views
0

Toast은 활동 테마와 마찬가지로 style.xml으로 스타일을 지정할 수 있습니까?style.xml에서 토스트 스타일을 정의하는 방법은 무엇입니까?

  • 텍스트 색상
  • 텍스트 크기
  • 텍스트 글꼴
  • 배경 색상/불투명도
  • 배경 모서리의 반경과 크기
:

나는 다음과 같은 스타일을 원하는

웹에서 토스트와 관련된 항목을 찾을 수 없습니다. 또는 style.xml

나는 Toast를 스타일에 쉽게 사용할 수있는 StyleableToast 클래스를 만들어서 해결했습니다! 당신이 당신의 자신의 토스트 레이아웃 만들기 위해 노력 해달라고 왜https://stackoverflow.com/a/39591755/5366495

+0

나는 가능하다고 생각합니다. 어쩌면 [이] (http://stackoverflow.com/questions/16909476/how-to-customize-toast-in-android) 도움이됩니다. – padeg

+0

@padeg이 링크를 알고 있습니다. 그러나'style.xml'에서이 작업을 수행 할 수 없다면 아무 것도 할 수 없습니다. – Muddz

+0

[this] (http://stackoverflow.com/a/18887238/6756523) 당신이 원하는 방식이 아닙니까? – padeg

답변

1

드로어 블 (레이아웃 등 팽창)을 Toast 스타일을, 나는 스타일링 많은 가능성과 완전한 Styleable Toast 클래스를 만들기 위해 다음 decicided // !

나는 Styleable Toast 클래스를 개선 유지하고 기능이 풍부한하고 그래서 여기를 인 dependency

로 추가 할 수 jCenter()에서이를 발표 할 예정이다. 프로젝트에 넣어 그냥 하나의 클래스 : StyleableToast로 만든 토스트의https://github.com/Muuddz/StyleableToast

예 :

enter image description here

모든 의견과 기능 요청을 환영합니다!

0

을 : 여기에 대답을 참조하십시오이 당신을 도울 것입니다

<LinearLayout android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/game_on"> 

    <TextView 
     android:id="@+id/toastMsg" 
     android:layout_margin="8dp" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="3dp" 
     android:text="Your text" 
     android:textSize="16dp" 
     android:layout_gravity="center_vertical"/> 

</LinearLayout> 

희망 : 여기

LayoutInflater inflater = getLayoutInflater(); 
View customToastroot = inflater.inflate(R.layout.custom_toast, null); 
TextView msg = (TextView) customToastroot.findViewById(R.id.toastMsg); 
msg.setText("Speed up !"); 
msg.setTypeface(tf); 
Toast customtoast = new Toast(getApplicationContext()); 
customtoast.setView(customToastroot); 
customtoast.setDuration(Toast.LENGTH_SHORT); 
customtoast.show(); 

을하고하면 custom_toast.xml입니다.

+0

이 솔루션은 stye.xml로 불가능할 경우 최후 대책이 될 것입니다. – Muddz

+0

이 솔루션을 사용하는 것이 더 편하다고 생각합니다. 사용자 정의와 함께 할 수 있습니다. –

+0

안녕하세요! 내 대답을 확인해. 많은 스타일에서 토스트 스타일을 만들 수있는'StyleableToast' 클래스를 만들었습니다. – Muddz

0

토스트 사용을 중지하고 SnackBar을 찾아야한다고 생각합니다. 토스트 형 메시지를 표시하는 머티리얼 디자인 가이드 라인의 새로운 표준입니다.

토스트와 비슷하지만 콘텐츠 표시 방법에 대한 레이아웃을 설정할 수도 있습니다.

Snackbar snackbar = Snackbar 
     .make(coordinatorLayout, "Welcome to AndroidHive", Snackbar.LENGTH_LONG); 

snackbar.show(); 

뿐만 아니라 SnackBar의 버튼 클릭과 같은 사용자 지정 상호 작용을 설정할 수도 있습니다. 예 :

Snackbar snackbar = Snackbar 
     .make(coordinatorLayout, "Message is deleted", Snackbar.LENGTH_LONG) 
     .setAction("UNDO", new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar snackbar1 = Snackbar.make(coordinatorLayout, "Message is restored!", Snackbar.LENGTH_SHORT); 
       snackbar1.show(); 
      } 
     }); 

snackbar.show(); 

다음은 문서를 작성하는 데 도움이되는 몇 가지 링크입니다.

1) Material Design guidelines for SnackBar

2) SnackBar examples

나는 이것이 당신이 당신이 당신의 질문에 요구 한 모든 것을 할 수 있습니다로 이동하는 가장 좋은 방법이라고 생각합니다.

+0

안녕하세요! 내 대답을 확인해. 많은 스타일에서 토스트 스타일을 만들 수있는'StyleableToast' 클래스를 만들었습니다. – Muddz

+0

상호 작용을 원하지 않는 한 수업이 더 나은 답변이라고 생각합니다. 하지만 Snackbar를 사용하면 버튼 클릭과 같은 상호 작용이 가능합니다. 또한 CoordinatorLayout을 사용하여 SnackBar를 이동할 수도 있습니다. Toast 메시지가 단순한 UI 디스플레이 이상의 것을하기를 원한다면 이것이 더 좋은 방법이라고 생각합니다. –

+0

안녕하세요. 난 그냥 내 애플 리케이션을위한 beutifull 토스트, 작은 중요한 메시지를 표시하고 싶지 않은 행동이나 버튼을 원한다. – Muddz

2

나는 그것에 대한 자신의 클래스를 만들었습니다. 다른 사람들의 해결책에서 경험 한 어려움에서 벗어날 수 있기 때문입니다.
이는 모습입니다 :

MyToast.showShort(context, getString(R.string.verworfen)); 
MyToast.showLong(context, getString(R.string.verworfen)); 

// 코드

public class MyToast{ 

    private static Toast currentToast; 

    public static void showShort(Context context, String message){ 
     if(currentToast != null) currentToast.cancel(); 
     LayoutInflater inflater = ((Activity)context).getLayoutInflater(); 
     View layout = inflater.inflate(R.layout.custom_toast, (ViewGroup) ((Activity) context).findViewById(R.id.root)); 
     TextView text = (TextView) layout.findViewById(R.id.message); 

     text.setText(message); 

     Toast toast = new Toast(context); 

     toast.setDuration(Toast.LENGTH_SHORT); 

     toast.setView(layout); 
     toast.show(); 
     currentToast = toast; 
    } 

    public static void showLong(Context context, String message){ 
     if(currentToast != null) currentToast.cancel(); 
     LayoutInflater inflater = ((Activity)context).getLayoutInflater(); 
     View layout = inflater.inflate(R.layout.custom_toast, (ViewGroup) ((Activity) context).findViewById(R.id.root)); 
     TextView text = (TextView) layout.findViewById(R.id.message); 

     text.setText(message); 

     Toast toast = new Toast(context); 

     toast.setDuration(Toast.LENGTH_LONG); 

     toast.setView(layout); 
     toast.show(); 
     currentToast = toast; 
    } 

    public static Toast getCurrentToast(){ 
     return currentToast; 
    } 

} 

// 레이아웃

: 단일 행에 토스트을 간단하게 표시 할 수 있습니다

Example

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

    <TextView 
     android:id="@id/message" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="12dp" 

     android:layout_gravity="center_horizontal" 
     android:textColor="@color/white" 
     android:textSize="14sp" /> 

</LinearLayout> 

는 쉽고 아닌 지저분한 방법이 없었기 때문에

<shape 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle" > 

    <solid 
     android:color="@color/primary_dark" > 
    </solid> 

    <stroke 
     android:width="2dp" 
     android:color="@color/primary_light" > 
    </stroke> 

    <padding 
     android:left="5dp" 
     android:top="5dp" 
     android:right="5dp" 
     android:bottom="5dp" > 
    </padding> 

    <corners 
     android:radius="11dp" > 
    </corners> 

</shape> 
+0

나는이 모든 것을 매우 간단한 방법으로 처리하는 클래스를 만들었지 만, 많은 다른 스타일 속성을 가진 Toast 스타일을 매우 쉽게 만듭니다. 내 대답을 확인해. – Muddz

관련 문제