android toast用法_android五种布局的特点

android toast用法_android五种布局的特点Toast用于向用户显示一些帮助/提示。下面我做了5中效果,来说明Toast的强大,定义一个属于你自己的Toast。1.默认效果代码Toast.makeText(getApplicationContext(),"默认Toast样式",     Toast.LENGTH_SHORT).show(); 2.自定义显示位置效果代码toast=Toast.mak…

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

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

Toast用于向用户显示一些帮助/提示。下面我做了5中效果,来说明Toast的强大,定义一个属于你自己的Toast。

1.默认效果

android toast用法_android五种布局的特点

代码

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

 

2.自定义显示位置效果

android toast用法_android五种布局的特点

代码

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

 

3.带图片效果

android toast用法_android五种布局的特点

 

代码

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.完全自定义效果

android toast用法_android五种布局的特点

代码

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.其他线程

android toast用法_android五种布局的特点

 代码

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>

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

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

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


相关推荐

  • keil5安装教程[通俗易懂]

    keil5安装教程[通俗易懂]keil5安装教程第一阶段:安装mdk第一阶段:安装mdk1、下载keil5的解压包,点击运行mdk514.exe文件2、在SetupMDK-ARMV5.14界面,点击Next3、勾选Iagree,点击Next4、自定义安装(1)记住界面弹出的默认安装的路径(2)点击Browse,选择自定义安装的位置,注意路径的命名尽量与默认路径的命名形式保持一致5、…

    2022年6月8日
    37
  • ip地址子网掩码默认网关dns的含义_子网掩码和默认网关的作用

    ip地址子网掩码默认网关dns的含义_子网掩码和默认网关的作用转载于:https://www.cnblogs.com/JuneWang/p/3917697.htmlIP地址,子网掩码,默认网关,DNS服务器是什么意思?(一)问题解析001.问:IP地址,子网掩码,默认网关,DNS服务器,有什么区别呀?我知道没有IP地址就不能上网,我也知道没设DNS就不能上外网,可它们都有什么功能,有什么区别呢?还有真奇怪,我的计算机没设DNS,竟然能上QQ,却不能打开网页,这是为什么呢>答:IP是32位二进制数据,通常以十进制表示,并以“.”…

    2022年9月29日
    2
  • pycharm上传代码到git_gitee收费吗

    pycharm上传代码到git_gitee收费吗前提:1、在码云https://gitee.com/ 中已经注册了账号,并且已经创建了仓库2、已经安装了git客户端: 3、在settings中设置gitee账户,登入:  、 3、在pycharm中安装gitee插件:file–setttings–plugins–搜索gitee,安装:  4、安装成功后检查:  5、提价本地项目代码到gitee:选中要提交的项目后VCS-…

    2022年8月26日
    5
  • 古今计算机发展简史思维导图_计算机功能演变史

    古今计算机发展简史思维导图_计算机功能演变史小时候,见过电子计算器,能很快完成计算,当时心想,计算器真是厉害,能提前存下所有数字的加减乘除结果。现在想来,很傻很天真。后来,吵着闹着要买小霸王学习机,最后如愿变成游戏机,经典游戏“超级玛丽”和“坦克大战”至今历历在目。

    2022年10月18日
    2
  • 多进程和多线程区别以及优缺点[通俗易懂]

    多进程和多线程区别以及优缺点[通俗易懂]多进程和多线程主要区别是:线程是进程的子集,一个进程可能由多个线程组成。多进程的数据是分开的、共享复杂,需要用IPC,但同步简单;多线程共享进程数据、共享简单,但同步复杂。多进程,window应用程序中消息有两种送出途径:直接和排队。Windows或某些运行的应用程序可直接发布消息给窗口过程,消息可送到消息列象连续不断轮询消息列队的OS中当前执行的每个进程,事件驱动不是由事件的顺序来控制的,而是由事件的发生来控,而事件的发生是随机的、不确定的,这就允许程序的用户用各种合理的顺序来安排程序的流程。多线

    2025年7月9日
    2
  • pycharm和idle语法区别_python文件无法用idle打开

    pycharm和idle语法区别_python文件无法用idle打开  最近在熟悉Python的class类的时候,无意中发现同样的代码,在pycharm和IDLE中结果不同,闲话少说先上代码:1classaa():2def__init__(self,name):3print(“mynameis%s”%name)4def__del__(self):5…

    2022年8月28日
    5

发表回复

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

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