Common.java 파일에 공통 메소드를 작성하십시오.
public static void displayCustomToast(Activity activity, String message, String length) {
// if you want to set typeface for toast text then use this //
Typeface tfShruti = Typeface.createFromAsset(activity.getAssets(), "fonts/shruti.ttf");
LayoutInflater inflater = activity.getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_layout,
(ViewGroup) activity.findViewById(R.id.custom_toast_layout_id));
// set a message
TextView text = (TextView) layout.findViewById(R.id.tv_toast);
text.setText(message);
text.setTypeface(tfShruti);
// Toast...
Toast toast = new Toast(activity.getApplicationContext());
toast.setGravity(Gravity.BOTTOM, 0, 0);
//toast.setMargin(0,10);
//toast.setGravity(Gravity.TOP | Gravity.LEFT, 40, 60);
//toast.setDuration(Toast.LENGTH_LONG);
if (length.equalsIgnoreCase("short")) {
toast.setDuration(Toast.LENGTH_SHORT);
} else {
toast.setDuration(Toast.LENGTH_LONG);
}
toast.setView(layout);
toast.show();
}
이와 같은 레이아웃을 만듭니다.
custom_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_toast_layout_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="@drawable/rectangle_fill_black_color"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="@+id/tv_toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
android:textColor="#ffffff"
android:textSize="17sp" />
</LinearLayout>
는이 같은 프로젝트에 어느 곳이 방법을 사용합니다.
Common.displayCustomToast(activity, message, "long");
또는
는
Common.displayCustomToast(activity, context.getResources().getString(R.string.please_check_internet), "short");
당신은', 토스트 토스트 = 새로운 토스트 (Activity.this)를 사용할 수 있습니다; toast.setView ("귀하의보기"); '이렇게. –
토스트의 소스 코드를 읽을 수 있습니다 : https://github.com/aosp-mirror/platform_frameworks_base/blob/master/core/java/android/widget/Toast.java – diegoveloper