Toast自定义

一、创建布局文件
toast.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/state"
        android:background="@drawable/dialog_bg"
        android:padding="12dp"
        android:textColor="@android:color/white"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="13dp"
        android:text="加载中"/>
</LinearLayout>

二、创建资源文件。
dialog_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#BC000000"/>
    <!-- 设置圆角半径 -->
    <corners android:radius="10dp"/>
</shape>

三、Activity中添加一下方法

    public void showToast(String text){
        View view = getLayoutInflater().inflate(R.layout.toast,null);//引用自定义布局
        ((TextView)view.findViewById(R.id.state)).setText(text);//展示信息
        Toast toast = Toast.makeText(this, text, Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER, 0, 0);//显示位置居中
        toast.setView(view);
        toast.show();
    }

四、调用
showToast("消息提示");
可新建Activity基类添加此方法,将其他的Activity都继承基类。这样的话所有的界面都可以用了
图示

posted @   LLj-511  阅读(15)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示