📜  在Android中将图像添加到Toast的不同方法

📅  最后修改于: 2021-05-10 14:33:21             🧑  作者: Mango

是一个反馈消息。当整个活动是交互式的并且对用户可见时,它只需要很小的显示空间。几秒钟后消失。它会自动消失。如果用户想要永久可见的消息,则可以使用“通知” 。 Toast的另一种类型是自定义Toast ,在其中可以使用图像代替简单的消息。因此,在本文中,我们将讨论三个 在Android上将图像添加到Toast的不同方法注意,我们将使用Java语言实现该项目。

方法1

步骤1:使用activity_main.xml文件

导航到应用程序> res>布局> activity_main.xml,然后将以下代码添加到该文件中。以下是activity_main.xml文件的代码。我们将在其中创建一个简单的TextView activity_main.xml文件。  

XML


      
    
  


Java
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
  
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    TextView show;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        show = findViewById(R.id.show);
          
        // on click on show text images toast will be shown
        show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                  
                // Initialising Toast
                Toast toast = new Toast(getApplicationContext());
                ImageView view = new ImageView(getApplicationContext());
                  
                // set image resource to be shown
                view.setImageResource(R.drawable.screenshot);
                  
                // setting view to toast
                toast.setView(view);
                  
                // showing toast
                toast.show();
            }
        });
    }
}


XML


  
    
  
    
  


Java
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
  
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    TextView show;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        show = findViewById(R.id.show);
        show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LayoutInflater inflater = getLayoutInflater();
                  
                // inflate layout file in Layout Inflater
                View view = inflater.inflate(R.layout.toast_image_layout, 
                        (ViewGroup) findViewById(R.id.relativeLayout1));
                Toast toast = new Toast(getApplicationContext());
                  
                // add view of toast to
                // toast_image_layout file
                toast.setView(view);
                  
                // show toast
                toast.show();
            }
        });
    }
}


Java
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
  
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    EditText msg;
    TextView show;
    ImageView image;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          
        show = findViewById(R.id.show);
          
        show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LinearLayout linearLayout = new LinearLayout(getApplicationContext());
                  
                // populate layout with your image and text
                // or whatever you want to put in here
                ImageView imageView = new ImageView(getApplicationContext());
                  
                // adding image to be shown 
                imageView.setImageResource(R.drawable.geeks);
                  
                // adding image to linearlayout
                linearLayout.addView(imageView);
                Toast toast = new Toast(getApplicationContext());
                  
                // showing toast on bottom
                toast.setGravity(Gravity.BOTTOM, 0, 0);
                toast.setDuration(Toast.LENGTH_LONG);
                  
                // setting view of toast to linear layout
                toast.setView(linearLayout);
                toast.show();
            }
        });
    }
}


步骤2:使用MainActivity。 Java文件

转到MainActivity。 Java文件并参考以下代码。下面是MainActivity的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。

Java

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
  
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    TextView show;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        show = findViewById(R.id.show);
          
        // on click on show text images toast will be shown
        show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                  
                // Initialising Toast
                Toast toast = new Toast(getApplicationContext());
                ImageView view = new ImageView(getApplicationContext());
                  
                // set image resource to be shown
                view.setImageResource(R.drawable.screenshot);
                  
                // setting view to toast
                toast.setView(view);
                  
                // showing toast
                toast.show();
            }
        });
    }
}

输出:

方法2

步骤1:使用activity_main.xml文件

activity_main.xml文件将是相同的。

步骤2:建立新的toast_image_layout.xml档案

转到应用程序> res>布局>右键单击>新建>布局资源文件,并将文件命名为toast_image_layout。以下是toast_image_layout.xml文件的代码。

XML格式



  
    
  
    
  

步骤3:使用MainActivity。 Java文件

转到MainActivity。 Java文件并参考以下代码。下面是MainActivity的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。

Java

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
  
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    TextView show;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        show = findViewById(R.id.show);
        show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LayoutInflater inflater = getLayoutInflater();
                  
                // inflate layout file in Layout Inflater
                View view = inflater.inflate(R.layout.toast_image_layout, 
                        (ViewGroup) findViewById(R.id.relativeLayout1));
                Toast toast = new Toast(getApplicationContext());
                  
                // add view of toast to
                // toast_image_layout file
                toast.setView(view);
                  
                // show toast
                toast.show();
            }
        });
    }
}

输出:

方法3

步骤1:使用activity_main.xml文件

activity_main.xml文件将是相同的。

步骤2:使用MainActivity。 Java文件

转到MainActivity。 Java文件并参考以下代码。下面是MainActivity的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。

Java

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
  
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    EditText msg;
    TextView show;
    ImageView image;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          
        show = findViewById(R.id.show);
          
        show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LinearLayout linearLayout = new LinearLayout(getApplicationContext());
                  
                // populate layout with your image and text
                // or whatever you want to put in here
                ImageView imageView = new ImageView(getApplicationContext());
                  
                // adding image to be shown 
                imageView.setImageResource(R.drawable.geeks);
                  
                // adding image to linearlayout
                linearLayout.addView(imageView);
                Toast toast = new Toast(getApplicationContext());
                  
                // showing toast on bottom
                toast.setGravity(Gravity.BOTTOM, 0, 0);
                toast.setDuration(Toast.LENGTH_LONG);
                  
                // setting view of toast to linear layout
                toast.setView(linearLayout);
                toast.show();
            }
        });
    }
}

输出: