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都继承基类。这样的话所有的界面都可以用了
图示
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)