ITSは本当に簡単あなたはトーストクラスに
を拡張することによってそれを行うことができます私のカスタムトーストcalssはトーストクラスを拡張したNexoolCustomToastあるとしましょう。以下はカスタムトーストクラスのコードです。このクラスの
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by Abhishek on 24-03-2017.
*/
/**
* By Default shows Error i.e. Fail toast
* */
public class NexoolCustomToast extends Toast {
private Context mContext;
private View mView;
private LayoutInflater mLayoutInflater;
private TextView mTitleTextView, mMessageTextView;
/**
* Construct an empty Toast object. You must call {@link #setView} before you
* can call {@link #show}.
*
* @param context The context to use. Usually your {@link Application}
* or {@link Activity} object.
*/
public NexoolCustomToast(Context context) {
super(context);
mContext = context;
mLayoutInflater = LayoutInflater.from(context);
mView = mLayoutInflater.inflate(R.layout.nexool_fail_custom_toast, null);
initialiseView(mView);
setView(mView);
setGravity(Gravity.TOP | Gravity.END, 0, 0);
}
public NexoolCustomToast(Context context, boolean state) {
super(context);
mContext = context;
mLayoutInflater = LayoutInflater.from(context);
if (state) {
mView = mLayoutInflater.inflate(R.layout.nexool_success_custom_toast, null);
} else {
mView = mLayoutInflater.inflate(R.layout.nexool_fail_custom_toast, null);
}
initialiseView(mView);
setView(mView);
setGravity(Gravity.TOP | Gravity.END, 0, 0);
}
private void initialiseView(View mView) {
mTitleTextView = (TextView) mView.findViewById(R.id.titleTextView);
mMessageTextView = (TextView) mView.findViewById(R.id.messageTextView);
}
public void setTitle(String title) {
if (title != null && title.length() != 0) {
mTitleTextView.setText(title);
} else {
mTitleTextView.setVisibility(View.GONE);
}
}
public void setMessage(String message) {
if (message != null && message.length() != 0) {
mMessageTextView.setText(message);
} else {
mMessageTextView.setVisibility(View.GONE);
}
}
@Override
public void show() {
super.show();
}
@Override
public void cancel() {
super.cancel();
}
public static NexoolCustomToast makeText(Context mContext, String mTitle, String mMessage) {
NexoolCustomToast mNexoolCustomToast = new NexoolCustomToast(mContext);
mNexoolCustomToast.setTitle(mTitle);
mNexoolCustomToast.setMessage(mMessage);
return mNexoolCustomToast;
}
public static NexoolCustomToast makeText(Context mContext, String mTitle, String mMessage, boolean state) {
NexoolCustomToast mNexoolCustomToast = new NexoolCustomToast(mContext, state);
mNexoolCustomToast.setTitle(mTitle);
mNexoolCustomToast.setMessage(mMessage);
return mNexoolCustomToast;
}
}
Iは、nexool_fail_custom_toast.xmlから4つのレイアウト画面サイズ(レイアウト大、レイアウト通常、レイアウト小、レイアウトXLARGE)とnexool_success_custom_toast.xmlをXMLレイアウトを使用しています。
//layout-large/nexool_fail_custom_toast.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@android:color/white"
android:layout_alignParentEnd="true"
android:layout_marginTop="?android:attr/actionBarSize"
android:layout_marginBottom="15dp"
android:layout_marginRight="15dp"
android:elevation="5dp">
<ImageButton
android:id="@+id/cancelToastImageButton"
android:layout_width="25dp"
android:layout_height="match_parent"
android:background="@android:color/holo_red_dark"
android:scaleType="centerInside"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="15dp">
<TextView
android:id="@+id/titleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Error Title"
android:minWidth="300sp"
android:textColor="@android:color/holo_red_dark"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/messageTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Error Message"
android:minWidth="300sp"
android:paddingTop="10dp"
android:textColor="@android:color/black"
android:textAppearance="?android:attr/textAppearanceSmall"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
//layout-large/nexool_success_custom_toast.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@android:color/white"
android:layout_alignParentEnd="true"
android:layout_marginTop="?android:attr/actionBarSize"
android:layout_marginBottom="15dp"
android:layout_marginRight="15dp"
android:elevation="5dp">
<ImageButton
android:id="@+id/cancelToastImageButton"
android:layout_width="25dp"
android:layout_height="match_parent"
android:background="@android:color/holo_green_light"
android:scaleType="centerInside"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="15dp">
<TextView
android:id="@+id/titleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Error Title"
android:minWidth="300sp"
android:textColor="@android:color/holo_green_light"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/messageTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Error Message"
android:minWidth="300sp"
android:paddingTop="10dp"
android:textColor="@android:color/black"
android:textAppearance="?android:attr/textAppearanceSmall"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
は今、これらのファイルの中にある景色のテキストサイズや幅と高さを変更することにより、レイアウト・小型、レイアウト、通常、レイアウトXLARGEのための2つのXMLファイルの上に作ります。
**赤い障害レイアウト
NexoolCustomToast.makeText(mContext, "Fail", "Fail Message", false).show();
//OR
NexoolCustomToast.makeText(mContext, "Fail", "Fail Message").show();
について緑の成功のレイアウト
NexoolCustomToast.makeText(mContext, "Success", "Success Message", true).show();
について**
を使用する方法
![Custom Toast](https://i.stack.imgur.com/Zjh4x.png)
あなたは、 'トーストトーストは=新しいトースト(Activity.this)を使用することができます。 toast.setView( "あなたのビュー"); 'これはこうです。 –
Toastのソースコードを読むことができます:https://github.com/aosp-mirror/platform_frameworks_base/blob/master/core/java/android/widget/Toast.java – diegoveloper