创建自定义吐司布局,如correct_toast.xml
:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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">
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF"
/>
</LinearLayout>
然后在Java代码中,构建与这一观点举杯:
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.correct_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();
这样,您可以更改背景的颜色,和/或更改文本的颜色。
http://stackoverflow.com/questions/6687666/android-如何设置颜色敬酒文本 –