2

我在尝试显示自定义吐司(带有图标和消息)时遇到了一些问题。这是代码。

private void makeToast(String text, int icon) {

        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.custom_toast,(ViewGroup) findViewById(R.id.message));
        ((TextView) layout.findViewById(R.id.message)).setText(text);

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

        // 0 - Exclamation, 1 - Noow icon
        if(icon == 0){
            ((ImageView) layout.findViewById(R.id.icon)).setImageResource(R.drawable.exclamation);
        }else if(icon == 1){
            ((ImageView) layout.findViewById(R.id.icon)).setImageResource(R.drawable.nicon);
        }


        Toast toast = new Toast(getBaseContext());
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(layout);
        toast.show();
    }

我想在第二个参数为0或1时更改图标和消息。我知道问题出在对第二个布局的调用中,但我不知道如何解决。

谢谢。

编辑:

我从 AsyncTask 中的 onPostExecute 调用此方法。我忘了告诉这个。

Thread [<11> AsyncTask #1] (Suspended (exception RuntimeException)) 
    ThreadPoolExecutor.runWorker(ThreadPoolExecutor$Worker) line: not available 
    ThreadPoolExecutor$Worker.run() line: not available 
    Thread.run() line: not available    

编辑2:

@Override
protected void onProgressUpdate(Void... values) {
    super.onProgressUpdate(values);
    makeToast("loading nightclubs...", 1);
    }
}

编辑3:

@Override
        protected void onPostExecute(List<Category> result) {
            Log.i("result", result.toString());

            // Cargamos el listado a pasarle a categoryId en la
            // consulta
            for (int k = 0; k < result.size(); k++) {
                ALLOFTHEM += result.get(k).getId();
                if (k < result.size() - 1) {
                    ALLOFTHEM += ",";
                }
            }

            Log.i("POST", ALLOFTHEM);
        }

解决了!!!

我用过...

runOnUiThread(new Runnable() {
                public void run() {
                 makeToast("loading nightclubs...", 1);
                }
            });

...每次我想在 UI 中祝酒,效果很好。

感谢大家。

4

4 回答 4

6

我的自定义 Toast 解决方案:

public class MyToast {
    public static void show(Context context, String text, boolean isLong) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View layout = inflater.inflate(R.layout.toast_layout, null);

        ImageView image = (ImageView) layout.findViewById(R.id.toast_image);
        image.setImageResource(R.drawable.ic_launcher);

        TextView textV = (TextView) layout.findViewById(R.id.toast_text);
        textV.setText(text);

        Toast toast = new Toast(context);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setDuration((isLong) ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT);
        toast.setView(layout);
        toast.show();
    }
}

和 toast_layout.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/toast_border"
    android:orientation="horizontal"
    android:padding="10dp" >

    <ImageView
        android:id="@+id/toast_image"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:contentDescription="@string/app_name"
        android:layout_marginRight="10dp" />

    <TextView
        android:id="@+id/toast_text"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:textColor="#FFF" />

</LinearLayout>

还有 toast_border.xml :

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <stroke
        android:width="2dp"
        android:color="#ee777777" />

    <solid android:color="#ee444444" />

    <padding
        android:bottom="2dp"
        android:left="20dp"
        android:right="20dp"
        android:top="2dp" />

    <corners android:radius="5dp" />
</shape>

结果图像:

在此处输入图像描述

我希望我对你有所帮助!

于 2013-05-28T09:53:12.567 回答
0

您将布局膨胀两次,并在第一个中设置文本,该文本被丢弃:

View layout = inflater.inflate(R.layout.custom_toast,(ViewGroup) findViewById(R.id.message));
((TextView) layout.findViewById(R.id.message)).setText(text);

// you discard the previously inflated layout, for which you have set text
layout = inflater.inflate(R.layout.custom_toast,(ViewGroup) findViewById(R.id.icon));

你想要的可能是这样的:

View layout = inflater.inflate(R.layout.custom_toast, null);
((TextView) layout.findViewById(R.id.message)).setText(text);

然后你的代码来设置图标。

于 2013-05-28T09:50:49.627 回答
0

使用这种方式:

        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.custom_toast_layout, null);

        TextView text = (TextView) layout.findViewById(R.id.toast_message);
        ImageView img = (ImageView) layout.findViewById(R.id.imageView1);

        if (icon == 0) {
            text.setText("icon 0");
            img.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));
        }

        if (icon == 1) {
            text.setText("icon 1");
            img.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher_one));
        }

        Toast toast = new Toast(getApplicationContext());
        toast.setGravity(Gravity.BOTTOM, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(layout);
        toast.show();
于 2013-05-28T09:55:25.753 回答
0

change your onPostExecute() method to

protected void onPostExecute(String file_url) {
            // anything else; you want to do here

            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                 makeToast("loading nightclubs...", 1);
                }
            });

        }
于 2013-05-28T10:19:50.683 回答