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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • idea2012.2 激活码-激活码分享

    (idea2012.2 激活码)本文适用于JetBrains家族所有ide,包括IntelliJidea,phpstorm,webstorm,pycharm,datagrip等。https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~D…

    2022年3月26日
    105
  • datax(8):TaskGroupContainer源码解读

    datax(8):TaskGroupContainer源码解读继续深挖datax里的container,任务一个任务进入datax都会判断是jobContainer还是TaskGroupContainer。那后者要做哪些事情。一,TaskGroupContainer概述JobContainer将所有的task分配到TaskGroup中执行,TaskGroup启动5个线程去消费所有的taskTaskGroupContainer里的主入口为start方法,实现自AbstractContainer.startTaskGroupContainer.start.

    2022年5月17日
    44
  • Spidermonkey_spider是什么意思

    Spidermonkey_spider是什么意思Slide1SpiderMonkey设计和实现Author:张平Email:p.zhang.9.25@gmail.comSlide2简介:SpiderMonkey:JavaScriptEngine:Javascript发明者BrendanEich在NetScape所写,后来由MozillaF​o​u​n​d​a​t​i​o​n​所

    2022年10月17日
    4
  • MATLAB实现线性插值interp1的功能

    MATLAB实现线性插值interp1的功能1.关于插值插值,它根据已知的数据序列(也可以理解为坐标中一连串离散的点),找到其中的规律;然后根据找到的这个规律,来对其中尚未有数据记录的点进数值的估计。2.关于线性插值线性插值是一种针对一维数据的插值方法,它根据一维数据序列中需要插值的点的左右邻近两个数据点来进行数值的估计。当然了它不是求这两个点数据大小的平均值(当然也有求平均值的情况),而是根据到这两个点的距离来分配它们的比重的。而对于一些边缘处的点也需要使用到外插:即通过找出最近的两个点,通过建立该两点之间的一元一次线性方程通过带入x即可以得

    2022年5月20日
    31
  • jsonobject是什么类型_json和jsonobject区别

    jsonobject是什么类型_json和jsonobject区别JSONObject是一种数据结构,可以理解为JSON格式的数据结构(key-value结构),可以使用put方法给json对象添加元素。JSONObject可以很方便的转换成字符串,也可以很方便的把其他对象转换成JSONObject对象。一、构建json1、实例化一个JSONObject对象,用put()方法将数据写入。JSONObjectobj=newJSONObject(…

    2022年9月2日
    5
  • python保留小数位的两种方法总结[通俗易懂]

    python保留小数位的两种方法总结[通俗易懂]题目背景:方法一:format函数format有不同用法,代码如下,前者使用了占位符{},使用占位符可以同时输出多个,后者一次只能输出一个importmathres=math.sqrt(a**2+b**2)#使用占位符print(‘{:.3f}’.format(res))#可以同时输出多个print(‘{:.3f}\n{:.2f}’.format(res,res))#不使用占位符只能输出一个print(format(res,’.3f’))运行结果:方法二:’%

    2022年8月12日
    4

发表回复

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

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