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>
相关推荐
云诗卡达7 小时前
Flutter安卓APP接入极光推送和本地通知
android·flutter
Jony_8 小时前
Android 设计架构演进历程
android·android jetpack
犹若故人归9 小时前
Android开发应用--高级UI界面设计
android·ui
zzhongcy10 小时前
复合索引 (item1, item2, item3 ) > (?, ?, ?) 不起作用,EXPLAIN 后type=ALL(全表扫描)
android·数据库
冬奇Lab11 小时前
稳定性性能系列之十三——CPU与I/O性能优化:Simpleperf与存储优化实战
android·性能优化
像风一样自由11 小时前
android native 中的函数动态注册方式总结
android·java·服务器·安卓逆向分析·native函数动态注册·.so文件分析
nono牛11 小时前
Makefile中打印变量
android
没有了遇见12 小时前
Android 关于RecycleView和ViewPager2去除边缘反馈
android
城东米粉儿12 小时前
android gzip数据压缩 笔记
android
城东米粉儿13 小时前
android 流量优化笔记
android