android自定义风格的toast

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

android自定义风格的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>


 

 

你可能感兴趣的:(android)