Toast制作的四种样式 (默认,自定义位置,图片+文字,自定义)

博客详细介绍了如何在Android开发中创建不同样式的Toast,包括默认样式、自定义显示位置、显示图片与文字结合的Toast,以及完全自定义的Toast实现方式,通过Activity1.java和activity1.xml布局文件进行示例展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 效果如图所示:



Activity1.java文件 (主入口文件)


package com.yongninggo.helloworld;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import static android.widget.Toast.makeText;
//程序主入口文件
public class Activity1 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity1);

        toast1();
        toptoast();
        imagetoast();
        zidingyi();
    }

    private void zidingyi() {
        findViewById(R.id.Toast4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                zidingyi44();
            }


        });
    }

    private void imagetoast() {
        findViewById(R.id.Toast3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imagetoast33();
            }
        });
    }

    private void toptoast() {
        findViewById(R.id.Toast2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toptoast22();
            }
        });
    }

    private void toast1() {
        findViewById(R.id.Toast1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toast11();
            }
        });
    }
    private void toast11() {
        makeText(this,"Toast",Toast.LENGTH_SHORT).show();
    }
    private void toptoast22() {
       Toast toast = Toast.makeText(this,"toptoast",Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.TOP,0,0);
        toast.show();
    }

    private void imagetoast33() {
        Toast toast = Toast.makeText(this,"imagetoast",Toast.LENGTH_SHORT);

        LinearLayout toast_layout = (LinearLayout) toast.getView();
        ImageView iv = new ImageView(this);
        iv.setImageResource(R.drawable.image1);
        toast_layout.addView(iv,0);

        toast.show();
    }

    private void zidingyi44() {
        LayoutInflater inflater = LayoutInflater.from(this);
        View toast_view = inflater.inflate(R.layout.toast_layout,null);
        Toast toast = new Toast(this);
        toast.setView(toast_view);
        toast.show();
    }
}


activity1.xml布局文件


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.yongninggo.helloworld.MainActivity"
    android:orientation="vertical"
    android:gravity="center_horizontal">

    <Button
        android:id="@+id/Toast1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="Toast"/>

    <Button
        android:id="@+id/Toast2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="topToast"/>

    <Button
        android:id="@+id/Toast3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="imagetoast"/>

    <Button
        android:id="@+id/Toast4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="自定义Toast"/>
</LinearLayout>


toast_layout.xml文件  (自定义toast布局文件)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal">

    <LinearLayout
        android:layout_width="120dip"
        android:layout_height="115dp"
        android:orientation="vertical"
        android:gravity="center_horizontal"
        android:layout_marginTop="100dip"
        android:background="#00000000">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="自定义Toast" />
        <ImageView
            android:id="@+id/image1"
            android:layout_width="100dip"
            android:layout_height="100dp"
            android:src="@drawable/image1"
            />
    </LinearLayout>


</LinearLayout>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值