android自定义风格的toast

先上图看一下我的自定义toast的样式


源码下载地址:

CustomToastActivity.java源码

package com.jinhoward.ui_customtoast;
/**
 * Author :jinhoward
 * Date:2013-07-21
 * Funtion: A toast of custom style.
 */

import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class CustomToastActivity extends Activity {
	private Button button;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_custom_toast);
		
		button=(Button)findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
			

			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				LayoutInflater inflater = getLayoutInflater();
				View customLayoutView = inflater.inflate(R.layout.toast_layout,
				                               null);

				Toast toast = new Toast(getApplicationContext());
				
				//设置toast为垂直居中显示
				toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
				toast.setDuration(Toast.LENGTH_LONG);
				toast.setView(customLayoutView);
				toast.show();
				
			}
		});

}
}

activity_custom_toast.xml源码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="#D1EEEE">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_gravity="center"
        android:text="显示自定义的Toast" />

</LinearLayout>

toast_layout.xml源码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toast_layout_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#DAAAAA"
    android:orientation="horizontal"
    android:padding="8dp" >

    <ImageView
        android:id="@+id/ImageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/lock" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true" 
        android:paddingLeft="10sp"
        android:gravity="top|left|center"
        android:layout_toRightOf="@+id/ImageView1"
        android:textStyle="bold"
        android:text="这是一个自定义的toast!"
        android:textColor="#FF0000" />

</RelativeLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值