自定义的五种Toast
2015-12-31 17:53
225 查看
自定义的五种Toast,希望能够帮助到大家,上代码!
首先是activity_main.xml里面的布局
编写好布局之后,我们就来看看自定义Toast到底是怎么写的
这样一来就能够显示了,dialog_custom.xml的话我是这样写的,当然这都是随意的
这样一来,完美的自定义Toast就能够实现了,都快试试吧。
首先是activity_main.xml里面的布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/button_1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/toast_btn_1" > </Button> <Button android:id="@+id/button_2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/toast_btn_2" > </Button> <Button android:id="@+id/button_3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/toast_btn_3" > </Button> <Button android:id="@+id/button_4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/toast_btn_4" > </Button> <Button android:id="@+id/button_5" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/toast_btn_5" > </Button> </LinearLayout>
编写好布局之后,我们就来看看自定义Toast到底是怎么写的
package com.mytoast; import java.util.Timer; import java.util.TimerTask; import android.app.Activity; import android.app.AlertDialog; import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; public class MyToast extends Activity implements OnClickListener { //初始化显示的信息 //初始化Button private Button toastBtn_1, toastBtn_2, toastBtn_3, toastBtn_4, toastBtn_5; private Toast toast = null; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); toastBtn_1 = (Button) findViewById(R.id.button_1); toastBtn_2 = (Button) findViewById(R.id.button_2); toastBtn_3 = (Button) findViewById(R.id.button_3); toastBtn_4 = (Button) findViewById(R.id.button_4); toastBtn_5 = (Button) findViewById(R.id.button_5); toastBtn_1.setOnClickListener(this); toastBtn_2.setOnClickListener(this); toastBtn_3.setOnClickListener(this); toastBtn_4.setOnClickListener(this); toastBtn_5.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub // AlertDialog.Builder builder; // AlertDialog dialog; switch (v.getId()) { case R.id.button_1: toast.makeText(this, "这是默认的Toast显示", Toast.LENGTH_LONG).show(); break; case R.id.button_2: toast = Toast.makeText(getApplicationContext(), "这是自定义位置的Toast显示", Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); break; case R.id.button_3: toast = Toast.makeText(getApplicationContext(), "这是带图片的Toast显示", Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 50, -100); LinearLayout layout = (LinearLayout) toast.getView(); ImageView image = new ImageView(getApplicationContext()); image.setImageResource(R.drawable.wallpaper_tree_small); layout.addView(image, 0); toast.show(); break; case R.id.button_4: View view = LayoutInflater.from(getApplicationContext()).inflate( R.layout.userdefinedtoast, null); toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.CENTER, 0, 0); toast.setView(view); // toast.show(); // 设置延迟五秒显示 myToast(); break; case R.id.button_5: View view1 = LayoutInflater.from(getApplicationContext()).inflate( R.layout.dialog_custom, null); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setView(view1); AlertDialog alertDialog = builder.create(); alertDialog.show(); break; } } // 设置延迟生效 private void myToast() { Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { initToast(); } }, 5000); } private void initToast() { toast.show(); } }
这样一来就能够显示了,dialog_custom.xml的话我是这样写的,当然这都是随意的
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toast_layout" android:layout_width="200dip" android:layout_height="fill_parent" android:background="#111111" android:orientation="vertical" > <TextView android:id="@+id/txt_Title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center|top" android:text="@string/toast_text_1" android:textColor="#ffffff" android:textSize="20sp" > </TextView> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#999999" android:orientation="horizontal" > <ImageView android:id="@+id/image_toast" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="10dip" android:src="@drawable/wallpaper_field_small" > </ImageView> <TextView android:id="@+id/txt_context" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center|right" android:text="@string/toast_text_2" android:textColor="#ffffff" android:textSize="15dip" > </TextView> </LinearLayout> </LinearLayout>
这样一来,完美的自定义Toast就能够实现了,都快试试吧。
相关文章推荐
- maple 教程
- chrome vim插件vimnum快捷键与使用
- Docker安装及配置管理
- UGUI之Selectable功能详解上篇(UI动画制作与实现)
- scrollRectToVisible 无效
- 程序员算法冒险之旅(一)---青蛙跳台阶问题
- easyui 界面框架小记
- 有时间开始写博客啦
- 获取当前时间所在的23:59:59
- iOS开发系列--通知与消息机制----转
- GDB调试core文件
- 马哥linux学习笔记:raid(维基百科)
- sql优化
- mysql cluster7.4.8安装和性能测试
- DWZ使用笔记
- AS技巧
- Android开源之五---优秀个人和团体篇
- <LeetCode OJ>Factorial Trailing Zeroes【172】
- hadoop报错 rmr: Cannot delete /login-dy-v10. Name node is in safe mode.
- javascript开发HTML5游戏--斗地主(单机模式part1)