Android Toast 預設和自定義使用
Toast是一種簡易的訊息提示框,和Dialog不一樣的是,Toast永遠不會獲得焦點,無法被點選。
Toast的設計思想就是儘可能不太引人注意,同時還能向使用者展示資訊,希望他們看到。
Toast顯示的時間有限,只分為long和short,它會根據使用者設定的顯示時間後自動消失。
1.預設Toast
myToast1("這是預設的Toast");
public void myToast1(String str) {
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
}
引數依次是顯示Toast的上下文、顯示的字串、顯示時間、最後show出來即可。
2.自定義Tosat
先大致瞭解一下LayoutInflater
作用:
1、對於一個沒有被載入或者想要動態載入的介面, 都需要使用inflate來載入.
2、對於一個已經載入的Activity, 就可以使用實現了這個Activiyt的的findViewById方法來獲得其中的介面元素.
方法:
1、Android裡面想要建立一個畫面的時候, 初學一般都是新建一個類, 繼承Activity基類, 然後在onCreate裡面使用setContentView方法來載入一個在xml裡定義好的介面.
2、其實在Activity裡面就使用了LayoutInflater來載入介面, 通過getSystemService(Context.LAYOUT_INFLATER_SERVICE)方法可以獲得一個 LayoutInflater, 也可以通過LayoutInflater inflater = getLayoutInflater();來獲得.然後使用inflate方法來載入layout的xml,
區別:
1、 LayoutInflater.inflate是載入一個佈局檔案。
2、 findViewById則是從佈局檔案中查詢一個控制元件;
myToast2("這是PM自定義的Toast");
public void myToast2(String str) {
LayoutInflater inflater = getLayoutInflater();
//作用就是將一個xml定義的佈局檔案例項化為view控制元件物件;
//1.reSource:View的layout的ID
//2.root:需要附加到resource資原始檔的根控制元件,inflate()會返回一個View物件,
// 如果第三個引數attachToRoot為true,就將這個root作為根物件返回,
// 否則僅僅將這個root物件的LayoutParams屬性附加到resource物件的根佈局物件上,也就是佈局檔案resource的最外層的View上。
// 如果root為null則會忽略view根物件的LayoutParams屬性(注意)。
//3.attachToRoot:是否將root附加到佈局檔案的根檢視上
View view = inflater.inflate(
R.layout.demo11_toast,
(ViewGroup) findViewById(R.id.R_toast),
false
);
//設定ImageView
ImageView img_toast = (ImageView) view.findViewById(R.id.img_toast);
img_toast.setImageResource(R.mipmap.ic_launcher);
//設定TextView
TextView tv_msg = (TextView) view.findViewById(R.id.tv_toast);
tv_msg.setText(str);
//上下文
Toast toast = new Toast(getApplicationContext());
//設定顯示位置,
toast.setGravity(Gravity.CENTER, 0, 0);
//設定時間
toast.setDuration(Toast.LENGTH_LONG);
//設定View
toast.setView(view);
//顯示Tosat
toast.show();
}
demo11_toast.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/R_toast"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/demo11_bg_toast">
<ImageView
android:id="@+id/img_toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv_toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/img_toast"
android:textSize="14sp" />
</RelativeLayout>
demo11_bg_toast.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!--設定透明背景色-->
<solid android:color="#3989cd" />
<!--設定黑色邊框-->
<stroke
android:width="1px"
android:color="#FFFFFF" />
<!--設定四個圓角半徑-->
<corners
android:bottomLeftRadius="50px"
android:bottomRightRadius="50px"
android:topLeftRadius="50px"
android:topRightRadius="50px" />
<!--設定邊距,讓空間大一點-->
<padding
android:bottom="4dp"
android:left="7dp"
android:right="7dp"
android:top="4dp" />
</shape>
好了,基本的Toast就這麼多,有錯誤請指正。
相關文章
- Android自定義ToastAndroidAST
- Android 程式設計程式碼-自定義 ToastAndroid程式設計AST
- Android 自定義Toast,修改Toast樣式和顯示時長AndroidAST
- Android 自定義Toast及BUGAndroidAST
- Android Toast 自定義背景、圖片 隨心使用AndroidAST
- 自定義ToastAST
- Android-重新包裝Toast,自定義背景AndroidAST
- Android中自定義特定顏色的ToastAndroidAST
- 自定義Toast樣式AST
- 【Android開發點滴】自定義Toast樣式AndroidAST
- Android自定義邊框背景顏色的ToastAndroidAST
- mui toast自定義樣式UIAST
- 自定義Toast樣式+改變Toast寬高AST
- 一個可以自定義的ToastAST
- 微信小程式之『自定義toast』微信小程式AST
- Android 自定義Toast實現多次觸發只會顯示一次toastAndroidAST
- 使用react-app-rewired和customize-cra對預設設定自定義ReactAPP
- 自定義Toast及視窗透明處理AST
- uni-app 自定義loading 自定義toast 相容小程式&APPAPPAST
- 百度小程式自定義通用toast元件AST元件
- 自定義Toast的背景顏色大小及字型大小AST
- MySQL使用之五_自定義函式和自定義過程MySql函式
- 自定義修改系統預設ie搜尋
- Android自定義View之Canvas的使用AndroidViewCanvas
- Android App中使用全域性ToastAndroidAPPAST
- Eclipse預設標籤TODO,XXX,FIXME和自定義標籤Eclipse
- Android自定義預定日曆,並且顯示陰曆Android
- Android 自定義viewAndroidView
- Android 自定義 TabLayoutAndroidTabLayout
- Android: 自定義ViewAndroidView
- Android 自定義 DrawableAndroid
- android自定義view(自定義數字鍵盤)AndroidView
- android自定義View&自定義ViewGroup(下)AndroidView
- android自定義View&自定義ViewGroup(上)AndroidView
- Android自定義控制元件——自定義屬性Android控制元件
- 如何使用Android自定義複合檢視Android
- android自定義鍵盤 自定義身份證鍵盤Android
- Android自定義控制元件之自定義屬性Android控制元件