更改“自定义吐司"中的文本和图标? [英] Change text and icon in a Custom Toast?

查看:76
本文介绍了更改“自定义吐司"中的文本和图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having some issues when I trying to show a custom toast (with icon and message). This is the code.

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时更改图标和消息.我知道问题出在第二个布局的调用中,但是我不知道如何解决.

I want to change the icon and message when the second parameter is 0 or 1. I know the problem is in the call to the second layout, but I don't know how to solve it.

谢谢.

我从AsyncTask中的onPostExecute调用此方法.我忘了告诉你.

I call this method from a onPostExecute in a AsyncTask. I forgot to tell this.

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    

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

@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中举杯,效果很好.

...everytime I want a toast in a UI and it works good.

感谢大家.

推荐答案

我的自定义Toast解决方案:

My solution for custom 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:

And 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:

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

图像结果:

希望我能对您有所帮助!

I hope I have helped you!

这篇关于更改“自定义吐司"中的文本和图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆