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


相关推荐

  • html5移动开发–js温馨提示

    html5移动开发–js温馨提示

    2022年1月15日
    45
  • Keil5下载安装教程并完成注册(配图操作)[通俗易懂]

    Keil5下载安装教程并完成注册(配图操作)[通俗易懂]Keil5安装教程以及安装包下载1、安装包下载2、下载并解压安装包,并按步骤完成安装2.1、运行安装程序,点击next2.2、勾选accept,点击next2.3、选择安装路径,点击next2.4、信息随意填写,点击next2.5、等待安装2.6、点击finish,完成安装3、打开注册机,完成注册3.1、以管理员身份运行keil53.2、点击licence3.3、打开注册机3.4、复制CID,选择arm,点击Generate3.5、复制生成的4步骤到keil5,点击ADD3.6、注册成功1、安装包下载微

    2022年5月23日
    60
  • 利用python进行数据分析豆瓣_如何用python做数据分析

    利用python进行数据分析豆瓣_如何用python做数据分析一、处理数据的基本内容数据分析是指对数据进行控制、处理、整理、分析的过程。在这里,“数据”是指结构化的数据,例如:记录、多维数组、Excel里的数据、关系型数据库中的数据、数据表等。二、说说

    2022年8月4日
    4
  • 简单的12864显示程序

    简单的12864显示程序12864

    2022年7月2日
    21
  • react路由守卫(路由拦截)

    react路由守卫(路由拦截)react不同于vue,通过在路由里设置meta元字符实现路由拦截。在使用Vue,框架提供了路由守卫功能,用来在进入某个路有前进行一些校验工作,如果校验失败,就跳转到404或者登陆页面,比如Vue中的beforeEnter函数:…router.beforeEach(async(to,from,next)=>{consttoPath=to.path;constfromPath=from.path;})…react实现路由拦截的基

    2022年5月22日
    494
  • C#数组反转

    C#数组反转staticvoidMain(string[]arr){string[]names={“老杨1″,”老李2″,”老王3″,”老牛4″,”老虎5″,”老磁6”};for(inti=0;i<names.Length/2;i++){…

    2022年6月8日
    45

发表回复

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

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