您可以使用自定义视图为自定义视图充气并使用toast.setView(布局).
例:
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
(ViewGroup) findViewById(R.id.toast_layout_root));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("This is a custom toast");
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
还有你的xml
android:id="@+id/toast_layout_root"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8dp"
android:background="#DAAA"
>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF"
/>
更多信息 @
在你的if和else部分代码(单独)中显示它以红色背景和白色文本颜色显示吐司.我没有看到任何问题.但是,如果您需要自定义,可以使用自定义布局并使布局膨胀并将视图设置为toast.
编辑:
你的textview
TextView text = (TextView) toast.getView().findViewById(android.R.id.message);
在if部分初始化,而在其他部分textview未初始化.
在if和else代码之外初始化textview.
检查这个名为crouton的库,您可能会发现它很有用