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)
上一篇 2025年11月2日 下午2:43
下一篇 2025年11月2日 下午3:15


相关推荐

  • 机器学习 BP神经网络(Python实现)

    机器学习 BP神经网络(Python实现)一个神经元即一个感知机模型,由多个神经元相互连接形成的网络,即神经网络。这里我们只讨论单隐层前馈神经网络,其连接形式入下:神经网络模型的待估参数即,每个神经元的阈值,以及神经元之间的连接权重。对于该模型有如下定义:训练集:D={(x1,y1),(x2,y2),……,(xm,ym)},x具有d个属性值,y具有k个可能取值则我们的神经网络(单隐层前馈神经网络…

    2025年10月26日
    4
  • css里的clear_css display属性的值及用法

    css里的clear_css display属性的值及用法clear:none|left|right|both.对于CSS的清除浮动(clear),一定要牢记:这个规则只能影响使用清除的元素本身,不能影响其他元素。清除浮动方法,1,给父级元素添加class=“clearflex”2,在css中给父级添加属性:overflow:hidden;(我比较喜欢这个)3,伪元素清除法,4,建立空的div,命名为clear,在css中添加clear:both;…

    2025年10月27日
    8
  • linux查看cuda版本「建议收藏」

    linux查看cuda版本「建议收藏」1.cat/usr/local/cuda/version.txt2.或者nvcc-v

    2022年6月11日
    63
  • Unicode编码转换工具_韓國萬聖節

    Unicode编码转换工具_韓國萬聖節/** * *@authorypan  *unicode编码转换工具类 *in2009-11-02 */publicclassParseUnicode{  //将unicode编码转换成中文 publicstaticStringdecodeUnicode(finalStringdataStr){   intstart=0;          intend

    2025年5月25日
    4
  • matlab极坐标画椭圆,Matlab极坐标曲线怎么绘制

    matlab极坐标画椭圆,Matlab极坐标曲线怎么绘制摘要腾兴网为您分享 Matlab 极坐标曲线怎么绘制 桌面时钟 易订货 微拍 玩客云等软件知识 以及天天气象 华图教育 app 金立手机软件商店 h5 编程软件 充点卷 绘声绘色软件 实达 bp690kii 新闻下 王者荣耀职业联赛直播 233 小游戏 app 帝国时代 3 短信备份和还原 防沉迷站 360 智能门锁 15 岁男孩子正常身高等软件 it 资讯 欢迎关注腾兴网 平面曲线和极坐标曲线是相对的 平面曲线

    2026年3月16日
    3
  • c# 将十进制数字转换成字节数组

    c# 将十进制数字转换成字节数组

    2021年9月11日
    215

发表回复

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

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