更改自定义Toast中的文本和图标?

时间:2013-05-28 09:44:07

标签: android toast

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

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 个答案:

答案 0 :(得分:5)

我的自定义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>

图片结果:

enter image description here

我希望我能帮到你!

答案 1 :(得分: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);

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

答案 2 :(得分: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();

答案 3 :(得分:0)

将您的onPostExecute()方法更改为

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);
                }
            });

        }