Andorid : Toast(弹出框)- 简单应用

复制代码
Toast  Android官方在Android API 30版本(或更高版本)之后即对该方法不生效。
只要SDK版本低于30,Toast.setGravity()方法即可生效

MainActivity.java

java 复制代码
package com.example.mytoast;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Context context;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;

        //Toast  Android官方在Android API 30版本(或更高版本)之后即对该方法不生效。
        //只要SDK版本低于30,Toast.setGravity()方法即可生效,

        Toast toast  = Toast.makeText(context,"您点击了按钮",Toast.LENGTH_SHORT);
       //弹出位置
        //Gravity.TOP 顶部 0 0
        //Gravity.CENTER_VERTICAL 中间
        toast.setGravity(Gravity.CENTER_VERTICAL,0,0);

        //自定义 toast
        /**
         * LayoutInflater它的作用类似于findViewById()。不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例化;
         * 而findViewById()是找xml布局文件下的具体widget控件(如 Button、TextView等)。
         * 具体作用:
         * 1、对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入;
         * 2、对于一个已经载入的界面,就可以使用Activiyt.findViewById()方法来获得其中的界面元素。
         * */
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        View view = layoutInflater.inflate(R.layout.toast_view,null);

        LinearLayout linearLayout = view.findViewById(R.id.ll_bg);
        linearLayout.setBackgroundColor(Color.BLUE);

        TextView textView =view.findViewById(R.id.btn_tv);
        textView.setText("自定义弹出框!");
        ImageView imageView = view.findViewById(R.id.image);
        imageView.setImageResource(R.mipmap.a);
        //设置视图
        toast.setView(view);
        
        //点击事件
        button = findViewById(R.id.btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击按钮 显示弹框
                toast.show();
            }
        });

    }
}

主布局 activity_main.xml

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"
    android:orientation="vertical"

    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮"
        android:textSize="24sp"
        />

</LinearLayout>

自定义Toast布局 toast_view.xml

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:id="@+id/ll_bg"
    android:layout_height="match_parent">


    <TextView
        android:id="@+id/btn_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        />

    <ImageView
        android:id="@+id/image"
        android:layout_width="68dp"
        android:layout_height="68dp"/>

</LinearLayout>
相关推荐
雨白2 小时前
Jetpack Compose Navigation 2.x 详解
android·android jetpack
Android系统攻城狮5 小时前
Android内核进阶之获取DMA地址snd_pcm_sgbuf_get_addr:用法实例(九十一)
android·pcm·android内核·音频进阶·pcm硬件参数
清空mega5 小时前
Android Studio移动应用基础教程(前言)
android·ide·android studio
2501_937145415 小时前
2025IPTV 源码优化版实测:双架构兼容 + 可视化运维
android·源码·源代码管理·机顶盒
zhoutanooi7 小时前
安卓bp文件编译学习
android·学习
aramae8 小时前
MySQL数据库入门指南
android·数据库·经验分享·笔记·mysql
百锦再9 小时前
选择Rust的理由:从内存管理到抛弃抽象
android·java·开发语言·后端·python·rust·go
whatever who cares9 小时前
在Java/Android中,List的属性和方法
android·java
油炸小波11 小时前
09-微服务原理篇(XXLJOB-幂等-MySQL)
android·mysql·微服务
果子没有六分钟12 小时前
setprop debug.hwui.profile visual_bars有什么作用
android