mainActivity如下:
[java]
package cn.c;
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.Toast;
//自定义Toast
public class MainActivity extends Activity {
private Button mButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton=(Button) findViewById(R.id.button);
mButton.setOnClickListener(new ClickListenerImpl());
}
private class ClickListenerImpl implements OnClickListener{
public void onClick(View v) {
Toast toast=Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT);
View toastView=
LayoutInflater.from(MainActivity.this).inflate(R.layout.toast, null);
toast.setView(toastView);
toast.show();
}
}
}
main.xml如下:
[html]
<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" >
<Button
android:id="@+id/button"
android:layout_width="150dip"
android:layout_height="50dip"
android:text="弹出Toast"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
toast.xml如下:
[html]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView"
android:layout_width="50dip"
android:layout_height="40dip"
android:src="@drawable/ic_launcher"
/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="40dip"
android:text="自定义的Toast"
android:gravity="center"
/>
</LinearLayout>