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>
相关推荐
AirDroid_cn8 小时前
Android 15 :如何让特定应用通知仅在锁屏显示横幅?
android·智能手机
zh_xuan8 小时前
android ARouter配置降级服务
android·arouter
常利兵8 小时前
Android开发秘籍:接口加解密全解析
android
xuboyok29 小时前
MySQL中ON DUPLICATE KEY UPDATE的介绍与使用、批量更新、存在即更新不存在则插入
android·数据库·mysql
羑悻的小杀马特9 小时前
LangChain实战:工具调用+结构化输出,让AI从“聊天“变“干活“
android·人工智能·langchain
秋饼10 小时前
[EXPLAIN:SQL 执行计划分析与性能优化实战]
android·sql·性能优化
robotx11 小时前
如何从framework层面跳过app开屏广告(简单模拟)
android
毕设源码-朱学姐11 小时前
【开题答辩全过程】以 基于Android的大学生兼职APP设计为例,包含答辩的问题和答案
android
tongxh42311 小时前
MySQL Workbench菜单汉化为中文
android·数据库·mysql
阿拉斯攀登11 小时前
第 3 篇 保姆级手把手!RK 安卓驱动开发环境搭建(Ubuntu20.04 + 官方 SDK),踩坑全规避
android·驱动开发·瑞芯微·rk安卓驱动