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>
相关推荐
小仙女喂得猪38 分钟前
2025 源码应用: Retrofit之动态更换BaseUrl
android·android studio
AmazingMQ1 小时前
Android 13----在framworks层映射一个物理按键
android
小李飞飞砖1 小时前
Android 插件化实现原理详解
android
m0_597345312 小时前
【Android】安卓四大组件之广播接收器(Broadcast Receiver):从基础到进阶
android·java·boradcast·安卓四大组件
whysqwhw2 小时前
OkHttp PublicSuffix包的后缀列表处理
android
yeziyfx2 小时前
kotlin中集合的用法
android·开发语言·kotlin
EngZegNgi4 小时前
安卓应用启动崩溃的问题排查记录
android·crash·启动崩溃
火柴就是我5 小时前
每日见闻之Container Decoration
android·flutter
天枢破军5 小时前
【AOSP】解决repo拉取提示无法连接android.googlesource.com
android
whysqwhw5 小时前
OkHttp之AndroidPlatform类分析
android