android toast居中显示_android Toast 弹出在屏幕中间位置以及自定义Toast

android toast居中显示_android Toast 弹出在屏幕中间位置以及自定义ToastToast我想我们应该使用的都很多,一般我们使用默认设置较多,但是默认设置往往不能满足我们的需求,那我们现在来自定义下:默认Toast:Toast.makeText(MainActivity.this,”点击按钮”,Toast.LENGTH_SHORT).show();设置Toast位置:通过setGravity设置Toast位置,可以是Gravity.CENTER:中间Gravity.BOTT…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

Toast 我想我们应该使用的都很多,一般我们使用默认设置较多,但是默认设置往往不能满足我们的需求,那我们现在来自定义下:

默认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(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;

}

}

对应的布局文件:此布局文件根据自己需求自定义

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:padding=”10dp”

android:layout_marginLeft=”10dp”

android:layout_marginRight=”10dp”

android:orientation=”vertical”>

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”/>

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” />

补充:

自定义Toast 填充满整个屏幕:

Toast toast2 = new Toast(MainActivity.this);

View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.toast_custom, null);

ImageView iv_toast = (ImageView) view.findViewById(R.id.iv_toast);

TextView tv_toast = (TextView) view.findViewById(R.id.tv_toast);

toast2.setView(view);

toast2.setGravity(Gravity.FILL_HORIZONTAL | Gravity.VERTICAL_GRAVITY_MASK, 0, 0);

toast2.show();

注意:布局文件要

android:layout_width=”match_parent”android:layout_height=”match_parent”

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/194581.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 单片机-控制-直流电机-基于L9110S-、L298N、TB6612FNG驱动[通俗易懂]

    直流电机(directcurrentmachine)能将直流电能转换成机械能(直流电动机)或将机械能转换成直流电能(直流发电机)的旋转电机。它是能实现直流电能和机械能互相转换的电机。当它作电动机运行时是直流电动机,将电能转换为机械能;作发电机运行时是直流发电机,将机械能转换为电能。直流电动机将直流电能转换为机械能的转动装置。电动…

    2022年4月13日
    301
  • 京东准点秒杀脚本[通俗易懂]

    京东准点秒杀脚本[通俗易懂]转载自:https://blog.csdn.net/tangcv/article/details/102993939直接上菜1.浏览器打开https://www.jd.com/,登录京东2.选择要抢购的商品3.按键盘F12,打开开发者模式,选择Console选项卡4.把以下代码粘贴在Console里面,修改代码里开始抢购时间(有二处时间)varnIn…

    2022年6月16日
    38
  • DSP28335 Flash烧写

    DSP28335 Flash烧写一.切换为Flash烧写版本方法//1.将主程序中的:MemCopy(&RamfuncsLoadStart,&RamfuncsLoadEnd,&RamfuncsRunStart);//InitFlash();//两个函数取消注释//2.将工程中的28335_RAM_lnk.cmd从工程中删除,添加CMD文件夹下的F283…

    2022年8月30日
    5
  • PyQt5 打包 没有icon图标

    PyQt5 打包 没有icon图标

    2021年11月22日
    47
  • LVS 负载均衡集群(一)| 超详细!一文带你了解 LVS 负载均衡集群

    LVS 负载均衡集群(一)| 超详细!一文带你了解 LVS 负载均衡集群前言如今,在各种互联网应用中,随着站点对硬件性能、响应速度、服务稳定性、数据可靠性等要求也越来越高,单台服务器也将难以无法承担所有的访问需求。当然了,除了使用性价比高的设备和专用负载分流设备外,还有一些其他选择来帮你解决此问题,就是搭建集群服务器通过整合多台普通的服务器设备并以同一个地址对外提供相同的服务,今天杰哥就带领大家学习企业中常用的一种群集技术——LVS。文章目录一、什么是LVS?…

    2022年7月24日
    14
  • 池化层的作用[通俗易懂]

    maxpooling是CNN当中的最大值池化操作,其实用法和卷积很类似tf.nn.max_pool(value,ksize,strides,padding,name=None)参数是四个,和卷积很类似:第一个参数value:需要池化的输入,一般池化层接在卷积层后面,所以输入通常是featuremap,依然是[batch,height,width,channels]这样的shape第二个参数ksize:池化窗口的大小,取一个四维向量,一般是[1,height,width,1],

    2022年4月10日
    51

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号