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>
相关推荐
硬件学长森哥3 小时前
Android影像基础--cameraAPI2核心流程
android·计算机视觉
前行的小黑炭7 小时前
Android 协程的使用:结合一个环境噪音检查功能的例子来玩玩
android·java·kotlin
阿华的代码王国7 小时前
【Android】内外部存储的读写
android·内外存储的读写
inmK111 小时前
蓝奏云官方版不好用?蓝云最后一版实测:轻量化 + 不限速(避更新坑) 蓝云、蓝奏云第三方安卓版、蓝云最后一版、蓝奏云无广告管理工具、安卓网盘轻量化 APP
android·工具·网盘工具
giaoho11 小时前
Android 热点开发的相关api总结
android
咖啡の猫13 小时前
Android开发-常用布局
android·gitee
程序员老刘13 小时前
Google突然“变脸“,2026年要给全球开发者上“紧箍咒“?
android·flutter·客户端
Tans513 小时前
Androidx Lifecycle 源码阅读笔记
android·android jetpack·源码阅读
雨白14 小时前
实现双向滑动的 ScalableImageView(下)
android
峥嵘life14 小时前
Android Studio新版本编译release版本apk实现
android·ide·android studio