0

我正在展示一个定制的吐司,带有一些生物的背景图像,但我希望吐司具有我定义的大小。

使用以下方法在普通吐司中是不可能的:

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>`

但是吐司的大小仍然相同,只是位置发生了变化。

4

2 回答 2

2

如果你想定制你的吐司,那么SuperToast 库就是答案。

它在定制方面灵活而无限!

于 2013-09-14T18:27:13.440 回答
1

我尝试改变尺寸,即宽度和高度。但似乎你永远不能显示指定宽度和高度的吐司......吐司根据内容改变形状,比如背景图像或里面的文字......这对于android中提供的默认吐司是正确的。

toast_layout您可以随时创建一个名为的小布局并将其可见性设置为不可见,而不是这样做。在您要显示的同一布局文件中创建它。把它放在你想让吐司出现的任何地方。

<RelativeLayout
        android:id="@+id/toast_layout"
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:background="@drawable/toastbackground"
        android:visibility="invisible" >

        <ImageView
            android:id="@+id/toast_img"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:src="@drawable/man" />

        <TextView
            android:id="@+id/toast_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/down_img"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#00ffff"
            android:textStyle="bold|italic" />

    </RelativeLayout>

在此处命名的 res/anim 文件夹中创建一个 tweenAnimation 文件,fade_in_out这与普通的 toast 不同,您可以根据需要自定义淡入和淡出属性。

然后,当您想在活动中显示 toast 时调用以下函数,例如:-

clone_toast(R.drawable.image,"Yao it Works...");

void clone_toast(int ids, String msg) {
        toast_img.setImageResource(ids);
        toast_text.setText(msg);
        toast_layot.setVisibility(View.VISIBLE);
        toast_layout.startAnimation(fade_in_out);
    }
于 2014-02-25T08:35:38.763 回答