Skip to content

Commit

Permalink
新增支持自定义Toast布局
Browse files Browse the repository at this point in the history
  • Loading branch information
880634 committed Sep 17, 2018
1 parent 922cf27 commit a0ed7de
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 32 deletions.
31 changes: 19 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,42 @@
> 已投入公司项目多时,没有任何毛病,可胜任任何需求,[点击此处下载Demo](https://raw.githubusercontent.com/getActivity/ToastUtils/master/ToastUtils.apk)
> 想了解实现原理的可以点击此链接查看ToastUtils源码:[ToastUtils](https://github.com/getActivity/ToastUtils/blob/master/library/src/main/java/com/hjq/bar/ToastUtils.java)
> 想了解实现原理的可以点击此链接查看ToastUtils源码:[ToastUtils](https://github.com/getActivity/ToastUtils/blob/master/library/src/main/java/com/hjq/toast/ToastUtils.java)
![](ToastUtils.gif)

#### 集成步骤

dependencies {
implementation 'com.hjq:toast:1.5'
implementation 'com.hjq:toast:2.0'
}

#### 初始化Toast

//建议在Application中初始化
//建议在Application中初始化
ToastUtils.init(getApplicationContext());

#### 显示Toast

ToastUtils.show("我是吐司");
ToastUtils.show("我是吐司");

#### 获取Toast对象

ToastUtils.getToast();

#### 设置Toast布局

ToastUtils.setView();

#### 自定义Toast样式

> 如果对Toast的默认样式不满意,可以在Application初始化样式,具体可参考[ToastBlackStyle](https://github.com/getActivity/ToastUtils/blob/master/library/src/main/java/com/hjq/bar/ToastBlackStyle.java)类的实现
> 如果对Toast的默认样式不满意,可以在Application初始化样式,具体可参考[ToastBlackStyle](https://github.com/getActivity/ToastUtils/blob/master/library/src/main/java/com/hjq/toast/ToastBlackStyle.java)类的实现
ToastUtils.initStyle(new IToastStyle());
ToastUtils.init(getApplicationContext());
ToastUtils.initStyle(new IToastStyle());

#### 框架亮点

* 功能强大:不分主次线程都可以弹出Toast,支持打印对象
* 功能强大:不分主次线程都可以弹出Toast,防崩溃处理

* 使用简单:只需传入文本,会自动根据文本长度决定吐司显示的时长

Expand All @@ -42,13 +49,13 @@

* 支持多种样式:默认为黑色样式,夜间模式可使用白色样式,还有仿QQ吐司样式

* 支持自定义样式:吐司(背景,圆角,重心,偏移),文字(大小,颜色,边距)
* 支持自定义样式:吐司(背景、圆角、重心、偏移),文字(大小、颜色、边距)

* 支持自定义扩展:可以在代码中获取Toast对象,可以调用Toast的任意API
* 支持自定义扩展:支持获取ToastUtils中的Toast对象,支持重新自定义Toast布局

* 框架兼容性良好:本框架不依赖任何第三方库,支持Eclipse和Studio的使用
* 支持全局配置样式:可以在Application中初始化Toast样式,达到一劳永逸的效果

* 支持全局配置样式:可以在Application中初始化ToastUtils样式,达到一劳永逸的效果
* 框架兼容性良好:本框架不依赖任何第三方库,支持Eclipse和Studio的集成使用

#### Android技术讨论Q群:78797078

Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "com.hjq.toast.demo"
minSdkVersion 14
targetSdkVersion 26
versionCode 15
versionName "1.5"
versionCode 20
versionName "2.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
11 changes: 4 additions & 7 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,19 @@ apply plugin: 'com.novoda.bintray-release'
android {
compileSdkVersion 26

resourcePrefix "ts_"

defaultConfig {
minSdkVersion 11
targetSdkVersion 26
versionCode 15
versionName "1.5"
versionCode 20
versionName "2.0"
}
}

publish {
userOrg = 'getactivity'//填写bintray用户名,注意大小写
groupId = 'com.hjq'//定义的maven group id最终引用形式
artifactId = 'toast'//maven的artifact id
version = '1.5'//maven 上发布版本号
description = 'A simple generic title bar'//描述,自己定义
version = '2.0'//maven 上发布版本号
description = 'This is a very functional Toast'//描述,自己定义
website = "https://github.com/getActivity/ToastUtils"//项目在github中的地址
}

Expand Down
46 changes: 35 additions & 11 deletions library/src/main/java/com/hjq/toast/ToastUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.os.Looper;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
Expand Down Expand Up @@ -46,12 +48,18 @@ public static void init(Context context) {
textView.setPadding(DimensUtils.dp2px(context, sDefaultStyle.getPaddingLeft()), DimensUtils.dp2px(context, sDefaultStyle.getPaddingTop()),
DimensUtils.dp2px(context, sDefaultStyle.getPaddingRight()), DimensUtils.dp2px(context, sDefaultStyle.getPaddingBottom()));
textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
textView.setBackgroundDrawable(drawable);
//setBackground API版本兼容
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
textView.setBackground(drawable);
}else {
textView.setBackgroundDrawable(drawable);
}

if (sDefaultStyle.getMaxLines() > 0) {
textView.setMaxLines(sDefaultStyle.getMaxLines());
}

sToast = new Toast(context);
sToast = new XToast(context);
sToast.setGravity(sDefaultStyle.getGravity(), sDefaultStyle.getXOffset(), sDefaultStyle.getYOffset());
sToast.setView(textView);
}
Expand All @@ -76,26 +84,32 @@ public static void show(Object object) {
*/
public static void show(CharSequence text) {

if (sToast == null || text == null || text.equals("")) return;
//吐司工具类还没有被初始化,必须要先调用init方法进行初始化
if (sToast == null) {
throw new IllegalStateException("ToastUtils has not been initialized");
}

if (text == null || text.equals("")) return;

//如果显示的文字超过了10个就显示长吐司,否则显示短吐司
//如果显示的文字超过了10个就显示长吐司,否则显示短吐司
if (text.length() > 20) {
sToast.setDuration(Toast.LENGTH_LONG);
} else {
sToast.setDuration(Toast.LENGTH_SHORT);
}

//子线程中异常情况处理
try {
((TextView) sToast.getView()).setText(text);
sToast.show();
} catch (RuntimeException e) {
try {
//判断是否在主线程中执行
if (Looper.myLooper() == Looper.getMainLooper()) {
sToast.setText(text);
sToast.show();
}else {
//在子线程中显示处理
Looper.prepare();
((TextView) sToast.getView()).setText(text);
sToast.setText(text);
sToast.show();
Looper.loop();
} catch (Exception ignored) {}
}
} catch (Exception ignored) {}
}

Expand All @@ -106,6 +120,13 @@ public static Toast getToast() {
return sToast;
}

/**
* 给当前Toast设置新的布局
*/
public static void setView(View view) {
sToast.setView(view);
}

/**
* 初始化Toast样式
*
Expand All @@ -115,6 +136,9 @@ public static void initStyle(IToastStyle style) {
ToastUtils.sDefaultStyle = style;
//如果吐司已经创建,就重新初始化吐司
if (sToast != null) {
//取消原有吐司的显示
sToast.cancel();
//重新初始化吐司类
init(sToast.getView().getContext().getApplicationContext());
}
}
Expand Down
54 changes: 54 additions & 0 deletions library/src/main/java/com/hjq/toast/XToast.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.hjq.toast;

import android.content.Context;
import android.os.Looper;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

/**
* author : HJQ
* github : https://github.com/getActivity/ToastUtils
* time : 2018/09/17
* desc : 加强版Toast
*/
final class XToast extends Toast {

private TextView mTextView;

XToast(Context context) {
this(context, Looper.getMainLooper());
}

XToast(Context context, Looper looper) {
super(context, looper);
}

@Override
public final void setView(View view) {
super.setView(view);
if (view instanceof TextView) {
mTextView = (TextView) view;
return;
}else if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
if (((ViewGroup) view).getChildAt(i) instanceof TextView) {
mTextView = (TextView) ((ViewGroup) view).getChildAt(i);
return;
}
}
}
//如果设置的布局没有包含一个TextView则抛出异常,必须要有一个TextView
throw new IllegalArgumentException("The layout must contain a TextView");
}

@Override
public final void setText(CharSequence s) {
if (mTextView != null) {
mTextView.setText(s);
}else {
super.setText(s);
}
}
}

0 comments on commit a0ed7de

Please sign in to comment.