網(wǎng)站首頁 編程語言 正文
原生Toast樣式
自定義Toast樣式
創(chuàng)建樣式
所謂自定義一個Toast就是建立一個布局文件,然后使用一個view容器承載,然后顯示出來。Toast畢竟只是起到一個提示作用,不必要放太多控件去填充,反而會顯得內(nèi)容很擁擠,一般一個標(biāo)題,一張圖片,一段簡短文字即可。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/white_radius_10"> <LinearLayout android:layout_width="120dp" android:layout_height="100dp" android:gravity="center" android:orientation="vertical" android:layout_gravity="center"> <ImageView android:layout_width="30dp" android:layout_height="30dp" android:src="@drawable/icon_error" /> <TextView android:id="@+id/ErrorTips" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center" android:text="密碼錯誤,請重試!" android:textColor="#099A9F" android:textSize="15sp" android:ellipsize="end"/> </LinearLayout> </FrameLayout>
封裝
因為我們在開發(fā)中需要頻繁使用Toast,所以使用一個類將其封裝起來,復(fù)用其功能,增強代碼可讀性,可行性。根據(jù)自己的開發(fā)需求,可以向外暴露修改內(nèi)容,圖片,顏色,文字大小等方法,由于Toast內(nèi)附控件較少,就可以直接將其控件ID和布局文件定義為常量引用。
public class ToastFormat {
private Context context;
private TextView tipsText;
private Toast toast = null;
private static final int ContentID = R.id.ErrorTips;
private static final int LayoutID = R.layout.passworderror;
public ToastFormat(Context context){
this.context = context;
}
public void InitToast(){
if (toast == null) {
toast = new Toast(context);
View view = LayoutInflater.from(context).inflate(LayoutID, null, false);
tipsText = view.findViewById(ContentID);
toast.setView(view);
}
}
public void setGravity(int gravity){
toast.setGravity(gravity, 0, 0);
}
public void setText(String tips){
tipsText.setText(tips);
}
public void show(){
toast.show();
}
public void setShowTime(int time){
toast.setDuration(time);
}
public void setTextColor(int color){
tipsText.setTextColor(context.getResources().getColor(color));
}
public void setTextSize(float size){
tipsText.setTextSize(size);
}
}
引用
定義對象
private ToastFormat format;
初始化
format = new ToastFormat(MainActivity.this);
format.InitToast();
設(shè)置顯示文字內(nèi)容
format.setText("自定義Toast");
最后顯示 Toast
format.show();
原文鏈接:https://blog.csdn.net/News53231323/article/details/123968086
相關(guān)推薦
- 2022-09-10 ELK收集Tomcat日志的實現(xiàn)_Tomcat
- 2022-04-05 提示:不建議在沒有為偏離分支指定合并策略時執(zhí)行 pull 操作。 您可以在執(zhí)行下一次
- 2022-06-29 C語言實例講解四大循環(huán)語句的使用_C 語言
- 2023-04-12 C++實現(xiàn)日期類的示例詳解_C#教程
- 2022-05-17 ubuntu Could NOT find CURL (missing: CURL_LIBRARY
- 2023-12-10 怎么給數(shù)據(jù)庫某個字段建立一個前綴索引
- 2023-03-22 Redis慢查詢?nèi)罩炯奥樵兎治鲈斀鈅Redis
- 2022-05-11 如何免安裝服務(wù)器將 React 整合進 Spring Boot
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支