0

寻找自定义吐司:

  1. 根据需要吐司的大小。
  2. 吐司的颜色。
  3. 吐司的位置。
  4. 以及吐司的时间根据需要(即会出现5秒或6秒)。
4

2 回答 2

0

Toast根据您选择的外观和感觉创建自定义布局。

custom_toast.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_toast_layout_id"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFF"
    android:orientation="horizontal"
    android:padding="5dp" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginRight="5dp" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:textColor="#000" />

</LinearLayout>

并在 Oncreate() 按钮中添加以下代码单击

 button = (Button) findViewById(R.id.buttonToast);

            button.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {

                    // get your custom_toast.xml ayout
                    LayoutInflater inflater = getLayoutInflater();

                    View layout = inflater.inflate(R.layout.custom_toast,
                      (ViewGroup) findViewById(R.id.custom_toast_layout_id));

                    // set a dummy image
                    ImageView image = (ImageView) layout.findViewById(R.id.image);
                    image.setImageResource(R.drawable.ic_launcher);

                    // set a message
                    TextView text = (TextView) layout.findViewById(R.id.text);
                    text.setText("Button is clicked!");

                    // Toast...
                    Toast toast = new Toast(getApplicationContext());
                    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                    toast.setDuration(Toast.LENGTH_LONG);
                    toast.setView(layout);
                    toast.show();


}
        });

单击按钮时,您将看到自定义Toast消息。

感谢http://www.mkyong.com/android/android-toast-example/

于 2013-08-01T13:10:39.170 回答
0

要在点击事件上进行自定义吐司,这很简单。只需通过 android 开发者网站 - http://developer.android.com/guide/topics/ui/notifiers/toasts.html#CustomToastView%20

或者您可以查看此示例 Toast 教程 - http://androiddesk.wordpress.com/2012/01/28/custom-notification-in-android-with-an-example/

于 2013-08-01T13:32:37.383 回答