Toast 显示 ImageView

① 新建工程
② 在drawable 文件夹中添加一副png 图片:argon.png

③ 修改mainActivity.java 文件

package zyf.EX_Ctrl_3_B;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.Toast;
public class EX_Ctrl_3_B extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*设置主屏布局*/
setContentView(R.layout.main);
/*创建新Toast对象*/
Toast showImageToast=new Toast(this);
/*创建新ImageView对象*/
ImageView imageView=new ImageView(this);
/*从资源中获取图片*/
imageView.setImageResource(R.drawable.argon);
/*设置Toast上的View--(ImageView)*/
showImageToast.setView(imageView);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Toast显示图片,你需要使用自定义Toast。以下是一个示例代码: ```java // 建立自定义Toast的布局文件,例如:toast_layout.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="12dp" android:background="#DAAA" > <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFF" android:text="This is a custom Toast" /> </LinearLayout> // 在代码中创建自定义Toast LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.custom_toast_layout)); ImageView image = (ImageView) layout.findViewById(R.id.imageView); image.setImageResource(R.drawable.ic_launcher); TextView text = (TextView) layout.findViewById(R.id.textView); text.setText("This is a custom Toast"); Toast toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); toast.show(); ``` 在此示例中,我们首先创建了一个自定义的布局文件toast_layout.xml,其中包含一个ImageView和一个TextView。然后,我们使用LayoutInflater从布局文件中创建一个视图。接着,我们设置ImageView的图像资源和TextView的文本内容。最后,我们创建一个Toast对象,将自定义的布局视图设置为Toast的视图,并显示它。 请注意,你需要将图像资源放在drawable文件夹中,并将其引用为R.drawable.ic_launcher。另外,也可以根据需要修改布局文件和代码来满足你的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值