标签:
通过Toast的setView(View view)方法可以将Toast的内容设为任意视图
eg:
toast_layout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/toast_layout_root" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/cover"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="I am a custom toast" android:textSize="20sp" android:typeface="monospace"/> </LinearLayout>
MainActivity.java
package com.panjn.userinterface; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.ViewGroup;
import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); View toastView = getLayoutInflater().inflate(R.layout.toast_layout,(ViewGroup)findViewById(R.id.toast_layout_root)); Toast toast = new Toast(getApplicationContext()); // toast.setText("Hello Minions"); //不能调用setText(String text), 否则抛出异常 toast.setView(toastView); toast.setDuration(Toast.LENGTH_LONG); //params: int gravity, int xOffset, int yOffset toast.setGravity(Gravity.CENTER,0,0); toast.show(); } }
标签:
原文地址:http://www.cnblogs.com/mmyz-sysu-panjn/p/toast.html