<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#000000" >    <Button         android:id="@+id/customToast"        android:layout_height="wrap_content"        android:layout_width="match_parent"        android:text="CustomToast"/>    </RelativeLayout>

这是main.xml,main.xml中的代码很简单就只有一个Button

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:padding="10dp"    android:background="@null" >        <ImageView         android:id="@+id/image"        android:layout_width="wrap_content"        android:layout_height="match_parent"        android:layout_marginRight="10dp"/>        <TextView         android:id="@+id/text"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textColor="#fff"/></LinearLayout>

这个就是自定义Toast的布局。需要的image和text在Java代码中自定义添加的!

package com.msr.toast;import android.app.Activity;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {    private Button customToast;        public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        customToast = (Button)findViewById(R.id.customToast);        customToast.setOnClickListener(new OnClickListener() {                        public void onClick(View v) {                Toast toast = new Toast(MainActivity.this);                LayoutInflater inflater = MainActivity.this.getLayoutInflater();                View toastLayout = inflater.inflate(R.layout.customtoast, null);                TextView text = (TextView)toastLayout.findViewById(R.id.text);                text.setText("You are my angle!");                ImageView image = (ImageView)toastLayout.findViewById(R.id.image);                image.setBackgroundResource(R.drawable.ic_launcher);                toast.setView(toastLayout);                toast.setDuration(Toast.LENGTH_LONG);                toast.show();            }        });    }}

这个就是Java中代码。 这个自定义感觉很有必要!特别是在提示什么的时候,如果还是系统自带的有点难看!

更多相关文章

  1. AndroidMenifest.xml(Android清单文件)内部代码分析
  2. Android布局文件中的属性含义
  3. Android 相对布局 RelativeLayout 属性
  4. Android漏洞——将Android恶意代码隐藏在图片中
  5. Android Studio 单刷《第一行代码》系列目录
  6. Android布局优化(三)使用AsyncLayoutInflater异步加载布局

随机推荐

  1. android eclipse 导入工程报错unable to
  2. Android(安卓)中Parcelable的作用 (转载)
  3. Android通信机制之Android架构(一)
  4. android异步处理Handler+Thread使用进阶
  5. Android总结(基础篇)-骚人文客
  6. Android自学笔记(4)
  7. android待机添加电量小部件,点击位置信息
  8. Opencv JNI Android(安卓)Camera 效率分
  9. 获取listview checkbox选择的值 android
  10. android之Intent的七大属性