나는 그것에 대한 자신의 클래스를 만들었습니다. 다른 사람들의 해결책에서 경험 한 어려움에서 벗어날 수 있기 때문입니다.
이는 모습입니다 :
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;
}
}
// 레이아웃
: 단일 행에 토스트을 간단하게 표시 할 수 있습니다
<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>
나는 가능하다고 생각합니다. 어쩌면 [이] (http://stackoverflow.com/questions/16909476/how-to-customize-toast-in-android) 도움이됩니다. – padeg
@padeg이 링크를 알고 있습니다. 그러나'style.xml'에서이 작업을 수행 할 수 없다면 아무 것도 할 수 없습니다. – Muddz
[this] (http://stackoverflow.com/a/18887238/6756523) 당신이 원하는 방식이 아닙니까? – padeg