Android UI控制元件系列:Toast(提示)
Toast用於向使用者顯示一些幫助/提示。下面我做了5中效果,來說明Toast的強大,定義一個屬於你自己的Toast。
注意:
LENGTH_LONG—長時間顯示檢視或文字提示
LENGTH_SHORT—短時間顯示檢視或文字提示
setGravity(int gravity,int xOffset,int yOffset)—設定提示應該在螢幕上的顯示的位置
setDuration(int duartion)—設定提示顯示的持續時間
1.預設效果
程式碼
Toast.makeText(getApplicationContext(), "預設Toast樣式", Toast.LENGTH_SHORT).show();
2.自定義顯示位置效果
程式碼
toast = Toast.makeText(getApplicationContext(), "自定義位置Toast", Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0); toast.show();
3.帶圖片效果
程式碼
toast = Toast.makeText(getApplicationContext(), "帶圖片的Toast", Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0); LinearLayout toastView = (LinearLayout) toast.getView(); ImageView imageCodeProject = new ImageView(getApplicationContext()); imageCodeProject.setImageResource(R.drawable.icon); toastView.addView(imageCodeProject, 0); toast.show();
4.完全自定義效果
程式碼
LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.custom, (ViewGroup) findViewById(R.id.llToast)); ImageView image = (ImageView) layout .findViewById(R.id.tvImageToast); image.setImageResource(R.drawable.icon); TextView title = (TextView) layout.findViewById(R.id.tvTitleToast); title.setText("Attention"); TextView text = (TextView) layout.findViewById(R.id.tvTextToast); text.setText("完全自定義Toast"); toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); toast.show();
5.其他執行緒
程式碼
new Thread(new Runnable() { public void run() { showToast(); } }).start();
完整程式碼
1.Main,java
package com.wjq.toast; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; public class Main extends Activity implements OnClickListener { Handler handler = new Handler(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findViewById(R.id.btnSimpleToast).setOnClickListener(this); findViewById(R.id.btnSimpleToastWithCustomPosition).setOnClickListener( this); findViewById(R.id.btnSimpleToastWithImage).setOnClickListener(this); findViewById(R.id.btnCustomToast).setOnClickListener(this); findViewById(R.id.btnRunToastFromOtherThread).setOnClickListener(this); } public void showToast() { handler.post(new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(), "我來自其他執行緒!", Toast.LENGTH_SHORT).show(); } }); } @Override public void onClick(View v) { Toast toast = null; switch (v.getId()) { case R.id.btnSimpleToast: Toast.makeText(getApplicationContext(), "預設Toast樣式", Toast.LENGTH_SHORT).show(); break; case R.id.btnSimpleToastWithCustomPosition: toast = Toast.makeText(getApplicationContext(), "自定義位置Toast", Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); break; case R.id.btnSimpleToastWithImage: toast = Toast.makeText(getApplicationContext(), "帶圖片的Toast", Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0); LinearLayout toastView = (LinearLayout) toast.getView(); ImageView imageCodeProject = new ImageView(getApplicationContext()); imageCodeProject.setImageResource(R.drawable.icon); toastView.addView(imageCodeProject, 0); toast.show(); break; case R.id.btnCustomToast: LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.custom, (ViewGroup) findViewById(R.id.llToast)); ImageView image = (ImageView) layout .findViewById(R.id.tvImageToast); image.setImageResource(R.drawable.icon); TextView title = (TextView) layout.findViewById(R.id.tvTitleToast); title.setText("Attention"); TextView text = (TextView) layout.findViewById(R.id.tvTextToast); text.setText("完全自定義Toast"); toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); toast.show(); break; case R.id.btnRunToastFromOtherThread: new Thread(new Runnable() { public void run() { showToast(); } }).start(); break; } } }
2.main,xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dip" android:gravity="center"> <Button android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/btnSimpleToast" android:text="預設"></Button> <Button android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="自定義顯示位置" android:id="@+id/btnSimpleToastWithCustomPosition"></Button> <Button android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/btnSimpleToastWithImage" android:text="帶圖片"></Button> <Button android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="完全自定義" android:id="@+id/btnCustomToast"></Button> <Button android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="其他執行緒" android:id="@+id/btnRunToastFromOtherThread"></Button> </LinearLayout>
3.custom.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="#ffffffff" android:orientation="vertical" android:id="@+id/llToast" > <TextView android:layout_height="wrap_content" android:layout_margin="1dip" android:textColor="#ffffffff" android:layout_width="fill_parent" android:gravity="center" android:background="#bb000000" android:id="@+id/tvTitleToast" /> <LinearLayout android:layout_height="wrap_content" android:orientation="vertical" android:id="@+id/llToastContent" android:layout_marginLeft="1dip" android:layout_marginRight="1dip" android:layout_marginBottom="1dip" android:layout_width="wrap_content" android:padding="15dip" android:background="#44000000" > <ImageView android:layout_height="wrap_content" android:layout_gravity="center" android:layout_width="wrap_content" android:id="@+id/tvImageToast" /> <TextView android:layout_height="wrap_content" android:paddingRight="10dip" android:paddingLeft="10dip" android:layout_width="wrap_content" android:gravity="center" android:textColor="#ff000000" android:id="@+id/tvTextToast" /> </LinearLayout> </LinearLayout>
相關文章
- Flutter Toast、彈出提示、輕提示FlutterAST
- Android自定義控制元件 帶文字提示的SeekBarAndroid控制元件
- Android 自定義Toast及BUGAndroidAST
- uview-ui toast 二次封裝ViewUIAST封裝
- [Android] Toast問題深度剖析(二)AndroidAST
- [Android] Toast問題深度剖析(一)AndroidAST
- android基礎學習-android篇day12-UI基礎控制元件(上)AndroidUI控制元件
- android基礎學習-android篇day13-UI基礎控制元件(下)AndroidUI控制元件
- Appium常用操作之「Toast提示資訊獲取」APPAST
- Android中自定義Toast文字大小AndroidAST
- 實現基於React的全域性提示元件ToastReact元件AST
- Android7.1.1Toast崩潰解決方案AndroidAST
- Android列表控制元件Android控制元件
- Android 分享控制元件Android控制元件
- android ios UIAndroidiOSUI
- android基礎學習-android篇day14-UI基礎控制元件綜合案例——點餐系統AndroidUI控制元件
- ToastAST
- SAP UI5 sap.ui.layout.Grid 控制元件概述UI控制元件
- Android 簡單控制元件Android控制元件
- 中級實訓Android學習記錄——Toast、AlertDialog、ProgressBarAndroidAST
- Delphi皮膚控制元件去NAG提示控制元件
- Android高亮引導控制元件Android控制元件
- Android - 控制元件抖動效果Android控制元件
- 深入學習SAP UI5框架程式碼系列之四:SAP UI5控制元件的後設資料實現UI框架控制元件
- iOS 使用UI控制元件的外觀協議UIAppearance進行設定預設UI控制元件樣式iOSUI控制元件協議APP
- Android效能UI卡頓AndroidUI
- Android 自定義UI元件AndroidUI元件
- 讓控制元件如此絲滑Scroller和VelocityTracker的API講解與實戰——Android高階UI控制元件APIAndroidUI
- 已開源!一款支援鴻蒙 NEXT Android iOS 的 UI 控制元件檢視器.md鴻蒙AndroidiOSUI控制元件
- SAP UI5 DynamicPage 控制元件介紹UI控制元件
- SAP UI5 FlexibleColumnLayout 控制元件介紹UIFlex控制元件
- 深入學習SAP UI5框架程式碼系列之六:SAP UI5控制元件資料繫結的實現原理UI框架控制元件
- Android常用控制元件-BannerView(無限輪播圖控制元件)Android控制元件View
- Android 控制元件架構與自定義控制元件詳解Android控制元件架構
- 自定義Toast樣式+改變Toast寬高AST
- Android控制元件的fitSystemWindows屬性Android控制元件Windows
- 【Android】自定義樹形控制元件Android控制元件
- van-toastAST
- Android UI——SpannableString詳細解析AndroidUI