寫一個布局檔案:
<?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="wrap_content"
android:background="@drawable/toast_view_bg"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:id="@+id/iv"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="使用者提示" />
</LinearLayout>
布局的布局寫一個shape檔案:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:useLevel="false" >
<solid android:color="#880000ff" />
<corners android:radius="5dp"/>
<size
android:height="50dp"
android:width="50dp" />
</shape>
代碼邏輯的實作:
寫一個BaseActivity或者在BaseFragment寫一個關于彈出toast的方法
public class BaseAbtivity extends Activity {
private TextView tv;
private View view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void showToast(String msg) {
Toast toast = new Toast(this);
view = View.inflate(this, R.layout.toast_view, null);
tv = (TextView) view.findViewById(R.id.tv);
tv.setText(msg);
toast.setView(view);
toast.show();
}
}
如果需要toast顯示更加多樣就自己修改布局就可以實作了。