android
原生Toast
源码并不多,照虎画猫的难度并不大。
刚好最近有一个需求是弹出的 toast
需要上面有一个图标,下面是文字。在屏幕上居中显示。
其实代码很少。就两个文件,一个是自定义的布局,一个是.java
文件。
custom_toast_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/wrapper_toast_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#000"
android:orientation="vertical"
android:padding="10dp">
<ImageView
android:id="@+id/img_toast_img"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:src="@mipmap/ic_launcher_round" />
<TextView
android:id="@+id/tv_toast_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="8dp"
android:textColor="#fff"
android:textSize="16sp"
tools:text="@string/app_name" />
</LinearLayout>
ToastHelper.java
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.abc.demo.R;
public class ToastHelper {
private ToastHelper() {
}
/**
* 直接使用,不需要使用 builder
*
* @param context context
* @param text text
* @param duration duration
*/
public static void show(Context context, CharSequence text, int duration) {
Toast toast = new Toast(context);
LayoutInflater inflate = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflate.inflate(R.layout.custom_toast_layout, null);
TextView tv = v.findViewById(R.id.tv_toast_text);
tv.setText(text);
toast.setView(v);
toast.setGravity(Gravity.CENTER,0,0);
toast.setDuration(duration);
toast.show();
}
/**
* 直接使用,不需要使用 builder
*
* @param context context
* @param text text
* @param duration duration
*/
public static void show(Context context, @StringRes int text, int duration) {
Toast toast = new Toast(context);
LayoutInflater inflate = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflate.inflate(R.layout.custom_toast_layout, null);
TextView tv = v.findViewById(R.id.tv_toast_text);
tv.setText(text);
toast.setView(v);
toast.setDuration(duration);
toast.setGravity(Gravity.CENTER,0,0);
toast.show();
}
public static class Builder {
private final View wrapperLayout;
private final Toast toast;
public Builder(Context context) {
LayoutInflater inflate = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
wrapperLayout = inflate.inflate(R.layout.custom_toast_layout, null);
toast = new Toast(context);
toast.setView(wrapperLayout);
}
public Builder setWrapperBackgroundColor(@ColorInt int color) {
wrapperLayout.setBackgroundColor(color);
return this;
}
public Builder setWrapperBackground(Drawable background) {
wrapperLayout.setBackground(background);
return this;
}
public Builder setWrapperBackgroundResource(@DrawableRes int resid) {
wrapperLayout.setBackgroundResource(resid);
return this;
}
public Builder setTextColor(@ColorInt int color) {
TextView tv = wrapperLayout.findViewById(R.id.tv_toast_text);
tv.setTextColor(color);
return this;
}
public Builder setTextSize(float size) {
TextView tv = wrapperLayout.findViewById(R.id.tv_toast_text);
tv.setTextSize(size);
return this;
}
public Builder setTextSize(int unit, float size) {
TextView tv = wrapperLayout.findViewById(R.id.tv_toast_text);
tv.setTextSize(unit, size);
return this;
}
public Builder setText(@StringRes int text) {
TextView tv = wrapperLayout.findViewById(R.id.tv_toast_text);
tv.setText(text);
return this;
}
public Builder setText(CharSequence text) {
TextView tv = wrapperLayout.findViewById(R.id.tv_toast_text);
tv.setText(text);
return this;
}
public Builder setIcon(@DrawableRes int resId) {
ImageView image = wrapperLayout.findViewById(R.id.img_toast_img);
image.setImageResource(resId);
return this;
}
@NonNull
public Context getContext() {
return this.wrapperLayout.getContext();
}
public Toast build() {
return toast;
}
public Toast show(@StringRes int text, int duration) {
Toast toast = build();
TextView tv = wrapperLayout.findViewById(R.id.tv_toast_text);
tv.setText(text);
// toast.setView(wrapperLayout);
toast.setDuration(duration);
toast.setGravity(Gravity.CENTER,0,0);
toast.show();
return toast;
}
public Toast show(CharSequence text, int duration) {
Toast toast = build();
TextView tv = wrapperLayout.findViewById(R.id.tv_toast_text);
tv.setText(text);
// toast.setView(wrapperLayout);
toast.setDuration(duration);
toast.setGravity(Gravity.CENTER,0,0);
toast.show();
return toast;
}
}
}
其实这里的Builder
有点鸡肋。一般来讲,这些属性完全应该在xml
里面写死。不过,说不定对于icon
可能要动态设置一下,如果仅仅是这样的话,可以只添加一个设置 icon 的方法,也可以不使用 Builder
.
此处的
Builder
也是对AlertDialog.Builder
的一次照虎画猫。
end: 发现对于这些类似于 feature
的东西,tag
比branch
好用。