android Toast大全(五种情形)建立属于你自己的Toast

Android Toast用法详解(各种自定义Toast) 4、完全实现我们自己的自定义效果:5、可以由其它线程更新:查看源代码:@Override@OverrideToast.makeText(getApplicationContext(), “这 是默认效果”,“这是自定义位置”, Toast.LENGTH_SHORT);Toast toast = Toast.makeText(getApplicationContext(), “这是带图片的”, 阅读详情
Toast用于向用户显示一些帮助/提示。下面我做了5中效果,来说明Toast的强大,定义一个属于你自己的Toast。

1.默认效果



代码

Toast.makeText(getApplicationContext(), "默认Toast样式",
Toast.LENGTH_SHORT).show();



2.自定义显示位置效果



代码

toast = Toast.makeText(getApplicationContext(),
"自定义位置Toast", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();



3.带图片效果





代码

toast = Toast.makeText(getApplicationContext(),
"带图片的Toast", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toastView = (LinearLayout) toast.getView();
ImageView imageCodeProject = new ImageView(getApplicationContext());
imageCodeProject.setImageResource(R.drawable.icon);
toastView.addView(imageCodeProject, 0);
toast.show();



4.完全自定义效果



代码

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom,
(ViewGroup) findViewById(R.id.llToast));
ImageView image = (ImageView) layout
.findViewById(R.id.tvImageToast);
image.setImageResource(R.drawable.icon);
TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
title.setText("Attention");
TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
text.setText("完全自定义Toast");
toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();



5.其他线程



代码

new Thread(new Runnable() {
public void run() {
showToast();
}
}).start();





完整代码

1.Main,java

package com.wjq.toast;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends Activity implements OnClickListener {
Handler handler = new Handler();

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

findViewById(R.id.btnSimpleToast).setOnClickListener(this);
findViewById(R.id.btnSimpleToastWithCustomPosition).setOnClickListener(
this);
findViewById(R.id.btnSimpleToastWithImage).setOnClickListener(this);
findViewById(R.id.btnCustomToast).setOnClickListener(this);
findViewById(R.id.btnRunToastFromOtherThread).setOnClickListener(this);

}

public void showToast() {
handler.post(new Runnable() {

@Override
public void run() {
Toast.makeText(getApplicationContext(), "我来自其他线程!",
Toast.LENGTH_SHORT).show();

}
});
}

@Override
public void onClick(View v) {
Toast toast = null;
switch (v.getId()) {
case R.id.btnSimpleToast:
Toast.makeText(getApplicationContext(), "默认Toast样式",
Toast.LENGTH_SHORT).show();
break;
case R.id.btnSimpleToastWithCustomPosition:
toast = Toast.makeText(getApplicationContext(),
"自定义位置Toast", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
break;
case R.id.btnSimpleToastWithImage:
toast = Toast.makeText(getApplicationContext(),
"带图片的Toast", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toastView = (LinearLayout) toast.getView();
ImageView imageCodeProject = new ImageView(getApplicationContext());
imageCodeProject.setImageResource(R.drawable.icon);
toastView.addView(imageCodeProject, 0);
toast.show();
break;
case R.id.btnCustomToast:
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom,
(ViewGroup) findViewById(R.id.llToast));
ImageView image = (ImageView) layout
.findViewById(R.id.tvImageToast);
image.setImageResource(R.drawable.icon);
TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
title.setText("Attention");
TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
text.setText("完全自定义Toast");
toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
break;
case R.id.btnRunToastFromOtherThread:
new Thread(new Runnable() {
public void run() {
showToast();
}
}).start();
break;

}

}
}



2.main,xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:padding="5dip" android:gravity="center">
<Button android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="@+id/btnSimpleToast"
android:text="默认"></Button>
<Button android:layout_height="wrap_content"
android:layout_width="fill_parent" android:text="自定义显示位置"
android:id="@+id/btnSimpleToastWithCustomPosition"></Button>
<Button android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="@+id/btnSimpleToastWithImage"
android:text="带图片"></Button>
<Button android:layout_height="wrap_content"
android:layout_width="fill_parent" android:text="完全自定义"
android:id="@+id/btnCustomToast"></Button>
<Button android:layout_height="wrap_content"
android:layout_width="fill_parent" android:text="其他线程"
android:id="@+id/btnRunToastFromOtherThread"></Button>

</LinearLayout>



3.custom.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:background="#ffffffff" android:orientation="vertical"
android:id="@+id/llToast" >
<TextView
android:layout_height="wrap_content"
android:layout_margin="1dip"
android:textColor="#ffffffff"
android:layout_width="fill_parent"
android:gravity="center"
android:background="#bb000000"
android:id="@+id/tvTitleToast" />
<LinearLayout
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/llToastContent"
android:layout_marginLeft="1dip"
android:layout_marginRight="1dip"
android:layout_marginBottom="1dip"
android:layout_width="wrap_content"
android:padding="15dip"
android:background="#44000000" >
<ImageView
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:id="@+id/tvImageToast" />
<TextView
android:layout_height="wrap_content"
android:paddingRight="10dip"
android:paddingLeft="10dip"
android:layout_width="wrap_content"
android:gravity="center"
android:textColor="#ff000000"
android:id="@+id/tvTextToast" />
</LinearLayout>
</LinearLayout>
Android入门第30天-Android里的Toast的使用 本篇带来的是: Android用于提示信息的一个控件——Toast(吐司)!Toast是一种很方便的消息提示框,会在 屏幕中显示一个消息提示框,没任何按钮,也不会获得焦点一段时间过后自动消失! 非常常用!我们通过一个例子把Toast的使用讲透! 阅读详情

相关推荐

Android:你真的会用Toast吗(介绍安卓好看简约的Toast快速解锁方法)

快速应用安卓开发当中的好看简约的Toast组件

m0_58941767的博客 1792

Android Toast的几种使用方式

Toast是Android中常用的组件,下面介绍下Toast使用的几种方式和注意事项。 Toast的使用方式简单来说有下面五种: 1、默认的显示 // 第一个参数:当前的上下文环境。可用getApplicationContext()或Activity的context // 第二个参数:要显示的字符串。也可是R.string中字符串ID // 第三个参数:显示的时间长短。Toast默认的有两个LENGTH_LONG(长)和LENGTH_SHORT(短),也可以使用毫秒如2000ms Toast

chali1314的博客 2万+

遥感数据处理之MCD19A2数据的处理

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 这一段时间一直在做modis气溶胶产品——MCD19A2这个数据的处理工作,总结了一些东西,在这里分享一下。 modis数据一般可以用MCTK和MRT这两个工具进行处理,下面先介绍一下用这两种工具处理MCD19A2的流程: 先解众惑:为什么使用MCD19A2数据处理出来的结果会有3个或4个或5个波段。 这里使用HDF Exploer这个软件,下载链接 1.使用MCTK处......

weixin_42176976的博客 1万+

Android 之 Toast 的基本使用

效果图关键代码//设置显示位置//设置字体颜色如果上面的那种还满足不了你的话,那么你完全可以自己写一个Toast的布局,然后显示出来;但是时间我们依旧控制不了!运行效果图关键代码还有自定义Toast的布局以及圆角背景:-- 设置透明背景色 -->-- 设置一个黑色边框 -->

leyang0910的博客 5230

【数据清洗】数据清洗流程及经验

预处理阶段 预处理阶段主要做两件事情: 一是将数据导入处理工具。 二是看数据。包含两个部分:一是看元数据,包括字段解释、数据来源、代码表等等一切描述数据的信息;二是抽取一部分数据,对数据本身有一个直观的了解,并且初步发现一些问题,为之后的处理做准备。 第一步:缺失值清洗 四个步骤: 1、确定缺失值范围:对每个字段都计算其缺失值比例,然后按照缺失比例和字段重要性,分别制定策略。 2、去除不需要的...

JoJo93723的博客 6492

深入解析Transformer三大注意力机制:自注意力、交叉注意力与因果自注意力

注意力机制是深度学习中的核心概念,它模拟了人类在处理信息时选择性聚焦的能力。其基本原理是通过计算查询与键的相似度,动态地为值分配权重,从而提取关键信息。这一机制解决了传统序列模型(如RNN)难以并行化和捕获长距离依赖的痛点,技术价值在于显著提升了模型的计算效率和表征能力。在自然语言处理、计算机视觉等应用场景中,注意力机制已成为构建强大模型的基石。本文聚焦于Transformer架构中的三种核心注意力变体:自注意力用于建立序列内部的全局关联,交叉注意力连接编码器与解码器的信息,而因果自注意力则确保自回归生成的

weixin_29094775的博客 410

android Toast大全(五种情形)建立属于你自己的Toast 收藏

<br /><br />  android Toast大全(五种情形)建立属于你自己的Toast 收藏<br />Toast用于向用户显示一些帮助/提示。下面我做了5中效果,来说明Toast的强大,定义一个属于你自己的Toast。<br />1.默认效果<br /><br />代码<br />Toast.makeText(getApplicationContext(), "默认Toast样式",<br />     Toast.LENGTH_SHORT).show();<br />2.自定义显示位置效果

gyflyx的专栏 905

yolov8的Detect层详解(输出维度改动)

对于部署端需要拿到onnx的output进行后处理,但是基于anchor_free的yolov8输出是1*(4+cls)*8400,不是我们常见的ncwh格式,所以对Detect层进行了学习,分享一下体会。

yjcccccc的博客 2万+
上一篇: Android开发中的Adapter内的onItemClick监听器四个arg参数问题
下一篇: java socket编程
轻鸿飘羽
博客等级 码龄16年 124粉丝 74原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值