是个传颂厨

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

 四种类型,普通,改位置,带图,自定义

首先布局中建四个相对应的按钮

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical"
        >
        <Button 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/toast_btn1"
            android:text="显示toast"
            />
                <Button 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/toast_btn2"
            android:text="改变位置toast"
            />
                <Button 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/toast_btn3"
            android:text="带图toast"
            />
                <Button 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/toast_btn4"
            android:text="完全自定义toast"
            />
    </LinearLayout>

</RelativeLayout>

然后顺便新建一个布局来放自定义的

<?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" >
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="静流"
        />
    <ImageView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/four"
        />
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="自定义的静流是我的"
        />

</LinearLayout>

然后写主文件

package com.example.deemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends Activity {

    private View toast_layout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initEvent();
    }
    
    private void initEvent(){//初始化点击事件(响应事件)
        findViewById(R.id.toast_btn1).setOnClickListener(new OnClickListener() {
        
            
            @Override
            public void onClick(View v) {
                showToast1();
                
            }
        });
        findViewById(R.id.toast_btn2).setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                showToast2();
                
            }
        });
        findViewById(R.id.toast_btn3).setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                showToast3();
                
            }
        });
findViewById(R.id.toast_btn4).setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                showToast4();
                
            }
        });
    }
    private void showToast1(){//显示默认toast
        Toast toast = Toast.makeText(this, "静流是我老婆",Toast.LENGTH_LONG);//最后一个是时间
        toast.show();
        //Toast.makeText(this, "默认",Toast.LENGTH_LONG).show();//写个别的方法玩玩。。。
    }
    private void showToast2(){//显示自定义位置toast
        Toast toast = Toast.makeText(this, "改变位置静流还是我老婆",Toast.LENGTH_LONG);
        toast.setGravity(Gravity.TOP, 0, 100);
        toast.show();
    }
    private void showToast3(){//带图片的
        Toast toast = Toast.makeText(this, "快看我老婆",Toast.LENGTH_LONG);
        LinearLayout toast_layut = (LinearLayout) toast.getView();
        ImageView iv = new ImageView(this);
        iv.setImageResource(R.drawable.four);
        toast_layut.addView(iv,1);//第二个参数为所处位置,可以改
        toast.show();
    }
    private void showToast4(){//完全自定义
        LayoutInflater inflater = LayoutInflater.from(this);
        View toast_view = inflater.inflate(R.layout.toast_layout, null);
        Toast toast = new Toast(this);
        toast.setView(toast_view);
        toast.show();
    }
}

这里按钮用的响应事件,其实拎出来初始化再用内部类也可以吧。

posted on 2016-08-06 23:43  是个传颂厨  阅读(257)  评论(0编辑  收藏  举报