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

Víc*_*tín 1 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();
    }
Run Code Online (Sandbox Code Playgroud)

我想在第二个参数为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    
Run Code Online (Sandbox Code Playgroud)

编辑2:

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

编辑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);
        }
Run Code Online (Sandbox Code Playgroud)

解决了!!!

我用过...

runOnUiThread(new Runnable() {
                public void run() {
                 makeToast("loading nightclubs...", 1);
                }
            });
Run Code Online (Sandbox Code Playgroud)

...每次我想要在UI中祝酒,它运作良好.

感谢大家.

lop*_*ael 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

和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>
Run Code Online (Sandbox Code Playgroud)

和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>
Run Code Online (Sandbox Code Playgroud)

图像结果:

在此输入图像描述

我希望我帮助过你!

  • 不,你没有帮助..他问不同:D (2认同)