我正在展示一个定制的吐司,带有一些生物的背景图像,但我希望吐司具有我定义的大小。
使用以下方法在普通吐司中是不可能的:
View view = toast.getView();
view.setBackgroundResource(R.drawable.go);
toast.setView(view);`
然后我像这样使用布局充气机:
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.hit_toast,
(ViewGroup) findViewById(R.id.custom_toast_layout));
layout.setBackgroundResource(R.drawable.go);
TextView text = (TextView) layout.findViewById(R.id.textToShow);
// Set the Text to show in TextView
text.setText(msg);
Toast toast = new Toast(getApplicationContext());
//toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(layout);
toast.show();
和我的xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_toast_layout"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:background="#DAAA"
android:gravity="center"
android:orientation="horizontal"
android:padding="8dp" >
<TextView
android:id="@+id/textToShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="ll"
android:textColor="#00ffff" />
</LinearLayout>`
但是吐司的大小仍然相同,只是位置发生了变化。