Toast

Toast是Android系统提供的一种非常好的提示方式,在程序中可以使用它将一些短小的信息通知给用户,这些信息会在一段时间后自动消失,并且不会占用任何的屏幕空间.

1、默认Toast

在屏幕底部显示提示内容

Toast.makeText(getApplicationContext(),"Toast",Toast.LENGTH_LONG).show();

2、使用setGravity()方法来定位Toast 在屏幕上的位置,例如toast.setGravity(Gravity.CENTER, 0, 0)可以把Toast 定位在屏幕中间。

//maleText返回的是toast
Toast toastCenter = Toast.makeText(getApplicationContext(),"ToastCenter",Toast.LENGTH_LONG);
toastCenter.setGravity(Gravity.CENTER,0,0);
toastCenter.show();

3、自定义Toast,图片加文字的形式显示,可以通过增加Toast 的Layout来调整Toast 上的布局方式。增加一个Toast 的Layout描述xml文件,/res/layout/toast_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#99000000"
android:gravity="center"> <LinearLayout
android:layout_width="200dp"
android:layout_height="200dp"
android:orientation="vertical"
android:gravity="center"> <ImageView
android:id="@+id/iv_toast"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="fitCenter"/> <TextView
android:id="@+id/tv_toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000"
android:layout_marginTop="15dp"/>
</LinearLayout> </LinearLayout>
Toast toastCustom = new Toast(getApplicationContext());
LayoutInflater inflater = LayoutInflater.from(ToastActivity.this);
View view = inflater.inflate(R.layout.layout_toast,null);
ImageView imageView = view.findViewById(R.id.iv_toast);
TextView textView = view.findViewById(R.id.tv_toast);
imageView.setImageResource(R.drawable.icon_lamp);
textView.setText("这是一个灯泡");
toastCustom.setView(view);
toastCustom.show();

完整代码:

layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <Button
android:id="@+id/btn_toast1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="默认"/> <Button
android:id="@+id/btn_toast2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="改变位置"/> <Button
android:id="@+id/btn_toast3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义"/> </LinearLayout>

ToastActivity

package com.example.helloworld;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast; public class ToastActivity extends AppCompatActivity { private Button mBtnToast1,mBtnToast2,mBtnToast3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_toast);
mBtnToast1 = findViewById(R.id.btn_toast1);
mBtnToast2 = findViewById(R.id.btn_toast2);
mBtnToast3 = findViewById(R.id.btn_toast3);
onClick onClick = new onClick();
mBtnToast1.setOnClickListener(onClick);
mBtnToast2.setOnClickListener(onClick);
mBtnToast3.setOnClickListener(onClick);
} class onClick implements View.OnClickListener{
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_toast1:
Toast.makeText(getApplicationContext(),"Toast",Toast.LENGTH_LONG).show();
break;
case R.id.btn_toast2:
//maleText返回的是toast
Toast toastCenter = Toast.makeText(getApplicationContext(),"ToastCenter",Toast.LENGTH_LONG);
toastCenter.setGravity(Gravity.CENTER,0,0);
toastCenter.show();
break;
case R.id.btn_toast3:
Toast toastCustom = new Toast(getApplicationContext());
LayoutInflater inflater = LayoutInflater.from(ToastActivity.this);
View view = inflater.inflate(R.layout.layout_toast,null);
ImageView imageView = view.findViewById(R.id.iv_toast);
TextView textView = view.findViewById(R.id.tv_toast);
imageView.setImageResource(R.drawable.icon_lamp);
textView.setText("这是一个灯泡");
toastCustom.setView(view);
toastCustom.show();
break;
} }
}
}

Android学习04的更多相关文章

  1. Android学习——windows下搭建Cygwin环境

    在上一篇博文<Android学习——windows下搭建NDK_r9环境>中,我们详细的讲解了在windows下进行Android NDK开发环境的配置,我们也讲到了在NDk r7以后,我 ...

  2. Android学习第三天-打包常用命令

    在前面<Android学习第一天-adb常用命令>和 <Android学习第二天-android常用命令>两篇博文中,我们重点讲解了adb和android的常用命令,下面我们讲 ...

  3. Android学习记录(三)——安装SQLite

    这次学习安装SQLite. 一.SQLite简介 重要特性:零配置,即不需要复杂的配置即可使用 详细:https://www.runoob.com/sqlite/sqlite-intro.html 二 ...

  4. Python学习--04条件控制与循环结构

    Python学习--04条件控制与循环结构 条件控制 在Python程序中,用if语句实现条件控制. 语法格式: if <条件判断1>: <执行1> elif <条件判断 ...

  5. Android学习路线总结,绝对干货

    title: Android学习路线总结,绝对干货 tags: Android学习路线,Android学习资料,怎么学习android grammar_cjkRuby: true --- 一.前言 不 ...

  6. Android 学习资源

    下面这些资源对Android开发来说是很有帮助的! 最常用的: Android开发官方网站:http://developer.android.com/index.html 这个网站应该是Android ...

  7. Android学习资料收集

    1.Android 学习之路 http://stormzhang.com/android/2014/07/07/learn-android-from-rookie/

  8. Android学习——第一个NDK程序

    在前面的学习中,我们已经讲解了关于NDK编程的环境搭建流程,简单的使用我们也通过官网本身自带的例子进行说明了.可是相信大家一定还存在这么的一个疑惑:“如果我要自己利用NDK编写一个Android应用, ...

  9. Android学习——windows下搭建NDK_r9环境

    1. NDK(Native Development Kit) 1.1 NDK简介 Android NDK是一套允许开发人员使用本地代码(如C/C++)进行Android APP功能开发的工具,通过这个 ...

随机推荐

  1. 基于Android的在线播放器系统的设计与实现

    文章结构: 1 引言 1.1系统的研究背景 现在的时代是互联网的时代,互联网高速发展的同时,无线网络也接入了互联网.社会的各个领域都已经被无线网络渗透.小的比如手机,电脑,电视.大的比如灯光系统,智能 ...

  2. z-index的展现形式

    没人告诉你关于z-index的一些事 堆叠顺序 z-index看上去很简单,z-index值大的元素在z-index值小的元素前面,对吧?但其实这只是z-index的一部分用法.很多程序猿都觉得很简单 ...

  3. echarts修改X、 Y坐标轴字体的颜色

    1.背景:在项目中常常会用到echarts的实例,根据不同的需求字体颜色需要变化,如图,要切合背景,就需要更改字体颜色 2.解决方案 xAxis : [ { type : 'category', da ...

  4. css的div动态水平垂直居中

      div动态水平垂直居中,思路如下: (1)先定位.如果相对于距离最近的父元素,用absolute:如果相对于body,用fixed. (2)然后,top和left都设为50%. (3)要居中的di ...

  5. java+layui的Excel导入导出

    html: <button class="layui-btn" onclick="exportData();">导出</button> ...

  6. onkeyup的死循环问题

    如果对一个控件调用的onkeyup事件,那么不能用回车来关闭alert()弹窗,因为按下回车的同时又再次触发了这个onkeyup事件,这样会造成一个死循环,不停按回车,不停的alert(), 所以应该 ...

  7. EAC3 Adaptive Hybrid Transform (AHT)

    adaptive hybrid transform 由两个linear transforms级联组成. 第一个transform为MDCT,MDCT使用KBD window产生256个transfor ...

  8. android:Android 6.0权限控制代码封装

    新建的Activity类可以继承这个Activity,这个类封装了关于新版的权限处理相关的代码 使用方法: package com.glsite.phone; import android.conte ...

  9. 概率dp (背包+概率) 背包的多一点

    题意:XX想抢劫银行,当危险率低于P的时候才能行动,现在给出每家银行的金钱mi和危险率pi,求最多能获得多少金钱: 题解:危险率是P,那么安全率就是1-P,那么XX抢劫的所有银行的安全率之积就不能小于 ...

  10. 吴裕雄 python 机器学习——局部线性嵌入LLE降维模型

    # -*- coding: utf-8 -*- import numpy as np import matplotlib.pyplot as plt from sklearn import datas ...