android Toast 弹出在屏幕中间位置以及自定义Toast
2018-03-21 13:37
405 查看
Toast 我想我们应该使用的都很多,一般我们使用默认设置较多,但是默认设置往往不能满足我们的需求,那我们现在来自定义下:
默认Toast:Toast.makeText(MainActivity.this,"点击按钮",Toast.LENGTH_SHORT).show();设置Toast位置:
通过setGravity设置Toast位置,可以是
Gravity.CENTER:中间
Gravity.BOTTOM:下方
Gravity.TOP:上方
Gravity.RIGHT:右边
Gravity.LEFT:左边
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.toast_custom, null);
toast2.setView(view);
toast2.setGravity(Gravity.CENTER, 0, 0);
toast2.show();
下面附上一个 ToastUtil类:public class ToastUitl {
private static Toast toast;
private static Toast toast2;
/**
* 初始化Toast(消息,时间)
*/
private static Toast initToast(CharSequence message, int duration) {
if (toast == null) {
toast = Toast.makeText(BaseApplication.getAppContext(), message, duration);
} else {
//设置文字
toast.setText(message);
//设置存续期间
toast.setDuration(duration);
}
return toast;
}
/**
* 短时间显示Toast(消息 String等)
*/
public static void showShort(CharSequence message) {
initToast(message, Toast.LENGTH_SHORT).show();
}
/**
* 短时间显示Toast(资源id)
*/
public static void showShort(int strResId) {
initToast(BaseApplication.getAppContext().getResources().getText(strResId), Toast.LENGTH_SHORT).show();
}
/**
* 长时间显示Toast(消息 String等)
*/
public static void showLong(CharSequence message) {
initToast(message, Toast.LENGTH_LONG).show();
}
/**
* 长时间显示Toast(资源id)
*/
public static void showLong(
4000
int strResId) {
initToast(BaseApplication.getAppContext().getResources().getText(strResId), Toast.LENGTH_LONG).show();
}
/**
* 自定义显示Toast时间(消息 String等,时间)
*/
public static void show(CharSequence message, int duration) {
initToast(message, duration).show();
}
/**
* 自定义显示Toast时间(消息 资源id,时间)
*/
public static void show(int strResId, int duration) {
initToast(BaseApplication.getAppContext().getResources().getText(strResId), duration).show();
}
/**
* 显示有image的toast 这是个view
*/
public static Toast showToastWithImg(final String tvStr, final int imageResource) {
if (toast2 == null) {
toast2 = new Toast(BaseApplication.getAppContext());
}
View view = LayoutInflater.from(BaseApplication.getAppContext()).inflate(R.layout.toast_custom, null);
TextView tv = (TextView) view.findViewById(R.id.toast_custom_tv);
tv.setText(TextUtils.isEmpty(tvStr) ? "" : tvStr);
ImageView iv = (ImageView) view.findViewById(R.id.toast_custom_iv);
if (imageResource > 0) {
iv.setVisibility(View.VISIBLE);
iv.setImageResource(imageResource);
} else {
iv.setVisibility(View.GONE);
}
toast2.setView(view);
toast2.setGravity(Gravity.CENTER, 0, 0);
toast2.show();
return toast2;
}
}对应的布局文件:此布局文件根据自己需求自定义 <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="vertical">
<ImageView
android:layout_marginTop="10dp"
android:id="@+id/toast_custom_iv"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@mipmap/ic_launcher"
android:layout_gravity="center"/>
<TextView
android:id="@+id/toast_custom_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textSize="16sp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="10dp"
android:textColor="#000"
tools:text="点击toast" />
</LinearLayout>
默认Toast:Toast.makeText(MainActivity.this,"点击按钮",Toast.LENGTH_SHORT).show();设置Toast位置:
通过setGravity设置Toast位置,可以是
Gravity.CENTER:中间
Gravity.BOTTOM:下方
Gravity.TOP:上方
Gravity.RIGHT:右边
Gravity.LEFT:左边
Toast toast = Toast.makeText(getApplicationContext(), "点击按钮", Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); toast.show();Toast 也可以是个布局:这个布局里可以是任何控件 图片 文字 等 Toast toast2 = new Toast(MainActivity.this);
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.toast_custom, null);
toast2.setView(view);
toast2.setGravity(Gravity.CENTER, 0, 0);
toast2.show();
下面附上一个 ToastUtil类:public class ToastUitl {
private static Toast toast;
private static Toast toast2;
/**
* 初始化Toast(消息,时间)
*/
private static Toast initToast(CharSequence message, int duration) {
if (toast == null) {
toast = Toast.makeText(BaseApplication.getAppContext(), message, duration);
} else {
//设置文字
toast.setText(message);
//设置存续期间
toast.setDuration(duration);
}
return toast;
}
/**
* 短时间显示Toast(消息 String等)
*/
public static void showShort(CharSequence message) {
initToast(message, Toast.LENGTH_SHORT).show();
}
/**
* 短时间显示Toast(资源id)
*/
public static void showShort(int strResId) {
initToast(BaseApplication.getAppContext().getResources().getText(strResId), Toast.LENGTH_SHORT).show();
}
/**
* 长时间显示Toast(消息 String等)
*/
public static void showLong(CharSequence message) {
initToast(message, Toast.LENGTH_LONG).show();
}
/**
* 长时间显示Toast(资源id)
*/
public static void showLong(
4000
int strResId) {
initToast(BaseApplication.getAppContext().getResources().getText(strResId), Toast.LENGTH_LONG).show();
}
/**
* 自定义显示Toast时间(消息 String等,时间)
*/
public static void show(CharSequence message, int duration) {
initToast(message, duration).show();
}
/**
* 自定义显示Toast时间(消息 资源id,时间)
*/
public static void show(int strResId, int duration) {
initToast(BaseApplication.getAppContext().getResources().getText(strResId), duration).show();
}
/**
* 显示有image的toast 这是个view
*/
public static Toast showToastWithImg(final String tvStr, final int imageResource) {
if (toast2 == null) {
toast2 = new Toast(BaseApplication.getAppContext());
}
View view = LayoutInflater.from(BaseApplication.getAppContext()).inflate(R.layout.toast_custom, null);
TextView tv = (TextView) view.findViewById(R.id.toast_custom_tv);
tv.setText(TextUtils.isEmpty(tvStr) ? "" : tvStr);
ImageView iv = (ImageView) view.findViewById(R.id.toast_custom_iv);
if (imageResource > 0) {
iv.setVisibility(View.VISIBLE);
iv.setImageResource(imageResource);
} else {
iv.setVisibility(View.GONE);
}
toast2.setView(view);
toast2.setGravity(Gravity.CENTER, 0, 0);
toast2.show();
return toast2;
}
}对应的布局文件:此布局文件根据自己需求自定义 <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="vertical">
<ImageView
android:layout_marginTop="10dp"
android:id="@+id/toast_custom_iv"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@mipmap/ic_launcher"
android:layout_gravity="center"/>
<TextView
android:id="@+id/toast_custom_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textSize="16sp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="10dp"
android:textColor="#000"
tools:text="点击toast" />
</LinearLayout>
相关文章推荐
- Android Toast 设置到屏幕中间以及其他自定义Toast的实现方法
- Android 自定义屏幕中间Toast的不延时弹出
- Android Toast 设置到屏幕中间,自定义Toast的实现方法,及其说明
- Android Toast 设置到屏幕中间,自定义Toast的实现方法,及其说明
- Android Toast 设置到屏幕中间,自定义Toast的实现方法,及其说明
- Android Toast 设置到屏幕中间,自定义Toast的实现方法,及其说明
- Android Toast 设置到屏幕中间,自定义Toast的实现方法,及其说明
- Android Toast 设置到屏幕中间,自定义Toast的实现方法
- Android Toast 设置到屏幕中间,自定义Toast的实现方法,及其说明
- Android Toast 设置到屏幕中间,自定义Toast的实现方法,及其说明
- Android Toast 设置到屏幕中间,自定义Toast的实现方法,及其说明
- android版百度地图 自定义 点击当前位置弹出气泡
- android Toast详解以及自定义Toast例子
- Android自定义dialog从屏幕底部弹出并且充满屏幕宽度
- 【Android】Android中设置Toast字体颜色及弹出的位置
- Android_屏幕翻转过程中Activity生命周期的变化,以及如果自定义屏幕翻转过程中执行的内容。
- Android项目实战--手机卫士30--读取应用的权限以及自定义Toast
- Android toast弹出时间自定义
- Android之PopupWindow-底部弹出,以及中间弹出有变暗效果
- (四十)android在代码中,如何设置自定义对话框在屏幕中的位置和大小