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


相关推荐

  • 黑盒测试用例设计 一[通俗易懂]

    黑盒测试用例设计 一[通俗易懂]简介: 总结黑盒测试用例的常用设计方法等价类划分一、方法简介1.定义把所有可能的输入数据,即程序的输入域划分成若干部分(子集),然后从每一个子集中选取少数具有代表性的数据作为测试用例2.划分等价类:等价类是指某个输入域的子集合。在该子集合中,各个输入数据对于揭露程序中的错误都是等效的。等价类划分可有两种不同的情况:有效等价类和无效等价类。(1)有效等价类是指对于程序的规格说明来说是…

    2022年6月12日
    32
  • vim常用命令总结[通俗易懂]

    vim常用命令总结[通俗易懂]先说明下,本文参考:https://www.cnblogs.com/yangjig/p/6014198.html总结的很好,话不多说,先上图在命令状态下对当前行用==(连按=两次),或对多行用

    2022年7月2日
    32
  • 如何在Python程序中运行Unix命令

    如何在Python程序中运行Unix命令Unix是由KenThompson和DennisRitchie在1969年左右在AT&T贝尔实验室开发的操作系统。我们可以使用许多有趣的Unix命令来执行不同的任务。问题是,我们可以直接在Python程序中使用此类命令吗?这就是我将在本教程中向您展示的内容。Unix命令ls列出目录中的所有文件。如果在Python脚本中按原样放置ls,则在运行程序时将得到以下内容:Tra…

    2022年5月10日
    38
  • 卸载LuDaShi时弹出“正在运行”“已被打开”的一种解决方法

    卸载LuDaShi时弹出“正在运行”“已被打开”的一种解决方法找软件资源的时候偷懒下载了三流网站的东西,结果被LDS(不知道是真LuDaShi还是山寨LuDaShi)缠上了。斗智斗勇一晚上,有了以下俩想法。(小白乱说不一定对)1、LDSGameMaster文件夹下,无法删除的子项疑似会在被用户选中执行删除命令时调用自身,以逃避卸载。笔者第一次选中LDSGameMaster时删除中断,显示有程序调用该文件夹,打开任务管理器后并未找到任何在运行中的LDS进程。一级一级打开子文件夹,发现最后不能被删除的子项,调用者是Win资源管理器——搁这儿搁这儿呢!于是试着改了下这个

    2022年5月30日
    72
  • 【信息学奥赛一本通】题解目录「建议收藏」

    【信息学奥赛一本通】题解目录「建议收藏」OJ网站:点击这里【语言及算法基础篇】第一部分:C++语言第一章:C++语言入门Hello,World!(信息学奥赛一本通-T1001):点击这里 输出第二个整数(信息学奥赛一本通-T1002):点击这里 对齐输出(信息学奥赛一本通-T1003):点击这里 字符三角形(信息学奥赛一本通-T1004):点击这里 地球人口承载力估计(信息学奥赛一本通-T1005):点击…

    2022年4月26日
    131
  • CANalyzer添加dbc文件「建议收藏」

    CANalyzer添加dbc文件「建议收藏」打开CANalyzer新建can文件选择Analysis&Stimulation->Configuration->DatabaseManagement选择通道Channels选择其中一个右键AddDatabase选择dbc文件进行添加

    2022年6月17日
    172

发表回复

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

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