仿ios的自定義toast炉媒。
說明:
一,使用的Androidstudio版本為3.4
二昆烁,使用說明吊骤。直接增加依賴。然后看本文第二大條静尼。
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.mamumu:mmToast:1.0'
}
展示效果:
現(xiàn)在正式開始
1白粉,核心activity和對應(yīng)的布局文件。
package com.mumu.toast;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
/**
* @author : zlf
* date : 2019-04-18
* github : https://github.com/mamumu
* blog : http://www.reibang.com/u/281e9668a5a6
*/
public class MMToast extends Toast {
public MMToast(Context context) {
super(context);
}
public static class Builder {
private Context context;
private String message;
private boolean isSuccess = true;
public Builder(Context context) {
this.context = context;
}
/**
* 設(shè)置toast的內(nèi)容
*
* @param message
* @return
*/
public Builder setMessage(String message) {
this.message = message;
return this;
}
/**
* 是否是顯示成功的toast鼠渺,true:成功 false:失敗
*
* @param isSuccess
* @return
*/
public Builder setSuccess(boolean isSuccess) {
this.isSuccess = isSuccess;
return this;
}
public MMToast create() {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.dialog_toast, null);
MMToast mmToast = new MMToast(context);
TextView msgText = view.findViewById(R.id.t_text);
ImageView imageView = view.findViewById(R.id.t_image);
if (!message.isEmpty()) {
msgText.setText(message);
}
if (isSuccess) {
imageView.setImageResource(R.drawable.ic_success);
} else {
imageView.setImageResource(R.drawable.ic_failure);
}
mmToast.setView(view);
mmToast.setDuration(Toast.LENGTH_SHORT);
mmToast.setGravity(Gravity.CENTER,0,0);
return mmToast;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialog_loading_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="70dp"
android:background="@drawable/shape_bg"
android:gravity="center"
android:orientation="vertical"
android:minWidth="80dp"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp">
<ImageView
android:id="@+id/t_image"
android:layout_width="30dp"
android:layout_height="30dp" />
<TextView
android:id="@+id/t_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="加載成功"
android:textColor="#ffffff"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
2鸭巴,如何使用。一拦盹,直接在自己的BaseActivity中增加showToastSuccess()和showToastFailure()方法鹃祖。如下第一段代碼所示。二普舆,MainActivity繼承BaseActivity恬口,直接使用對應(yīng)的方法。
package com.mumu.mmtoast.base;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import com.mumu.toast.MMToast;
/**
* @author : zlf
* date : 2019/4/17
* github : https://github.com/mamumu
* blog : http://www.reibang.com/u/281e9668a5a6
*/
public class BaseActivity extends AppCompatActivity {
private MMToast mmToast;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
}
protected void showToastSuccess(String msg) {
if (mmToast == null) {
MMToast.Builder builder=new MMToast.Builder(this)
.setMessage(msg)
.setSuccess(true);
mmToast=builder.create();
}else {
mmToast.cancel();
MMToast.Builder builder=new MMToast.Builder(this)
.setMessage(msg)
.setSuccess(true);
mmToast=builder.create();
}
mmToast.show();
}
protected void showToastFailure(String msg) {
if (mmToast == null) {
MMToast.Builder builder=new MMToast.Builder(this)
.setMessage(msg)
.setSuccess(false);
mmToast=builder.create();
}else {
mmToast.cancel();
MMToast.Builder builder=new MMToast.Builder(this)
.setMessage(msg)
.setSuccess(false);
mmToast=builder.create();
}
mmToast.show();
}
}
package com.mumu.mmtoast;
import android.os.Bundle;
import android.view.View;
import com.mumu.mmtoast.base.BaseActivity;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class MainActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
initView();
}
private void initView() {
}
@OnClick({R.id.button3, R.id.button4})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.button3:
showToastSuccess("加載成功");
break;
case R.id.button4:
showToastFailure("加載失敗");
break;
}
}
}
3沼侣,對應(yīng)github地址
demo地址:https://github.com/mamumu/mmToast
4祖能,我后面將dialog(一),loading(二)和toast(三)做了統(tǒng)一封裝鏈接如下蛾洛,推薦使用:
http://www.reibang.com/p/9259ad7f857b
如果有發(fā)現(xiàn)錯(cuò)誤歡迎指正我及時(shí)修改养铸,如果有好的建議歡迎留言。如果覺得對你有幫助歡迎給小星星,謝謝揭厚。