Android自定义Toast

2014-11-24 07:37:40 · 作者: · 浏览: 2

Android自定义Toast


步骤如下:


1 定义Toast的样式 toast.xml


< xml version="1.0" encoding="utf-8" >
android:id="@+id/hahahahhahah"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/black"
android:padding="6dp" >


android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >


android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/app_name"
android:src="@drawable/android" >


android:id="@+id/toastTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="4dp"
android:text="这是标题栏"
android:textColor="@color/white"
android:textIsSelectable="true"
android:textSize="20sp" />


android:id="@+id/toastTextView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"
android:paddingBottom="4dp"
android:text="这是要显示的内容"
android:textColor="@color/white"
android:textIsSelectable="true"
android:textSize="12sp" />



2 自定义Toast,引用我们自己的布局


View contentView = (LinearLayout) LayoutInflater
.from(this).inflate(R.layout.toast, null);
Toast toast=new Toast(this);
toast.setGravity(Gravity.CENTER,0,0);
toast.setView(contentView);
toast.setDuration(Toast.LENGTH_LONG);
toast.show();


这样就显示出刚才看到的效果了,大家可以在加入监听器等其他的内容