Android 更改 Toast 的默認(rèn)位置方法
Android中Toast的默認(rèn)位置在屏幕靠近底部的位置,這個(gè)默認(rèn)位置有時(shí)候并不合適。比如頁面上內(nèi)容較少時(shí),內(nèi)容一般集中在屏幕上半部分,用戶的注意力也集中在屏幕上半部分,默認(rèn)位置的Toast用戶可能沒有注意到。還有可能是默認(rèn)位置的Toast被用戶的手擋住了。實(shí)踐中感覺將Toast顯示在屏幕的中部或中上部會(huì)比較好。如何修改Toast的默認(rèn)位置呢?下面做一個(gè)簡單的例子來演示一下。
先上截圖:
布局文件activity_toast.xml代碼如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onClickDefaultToast" android:text="點(diǎn)擊顯示默認(rèn)位置的Toast" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onClickCenterToast" android:text="點(diǎn)擊顯示居中位置的Toast" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onClickTopToast" android:text="點(diǎn)擊顯示居中上部位置的Toast" /> </LinearLayout>
后臺(tái)ToastActivity.java代碼如下:
package chengyujia.demo.aty; import android.os.Bundle; import android.view.Display; import android.view.Gravity; import android.view.View; import android.widget.Toast; import chengyujia.demo.R; public class ToastActivity extends BaseActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_toast); } public void onClickDefaultToast(View v) { Toast.makeText(this, "默認(rèn)位置的Toast", Toast.LENGTH_LONG).show(); } public void onClickCenterToast(View v) { Toast toast = Toast.makeText(this, "居中位置的Toast", Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); } public void onClickTopToast(View v) { Display display = getWindowManager().getDefaultDisplay(); // 獲取屏幕高度 int height = display.getHeight(); Toast toast = Toast.makeText(this, "居中上部位置的Toast", Toast.LENGTH_LONG); // 這里給了一個(gè)1/4屏幕高度的y軸偏移量 toast.setGravity(Gravity.TOP, 0, height / 4); toast.show(); } }
以上這篇Android 更改 Toast 的默認(rèn)位置方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)購物車整體頁面邏輯詳解
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)購物車的整體頁面邏輯,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11Android編程實(shí)現(xiàn)圖片放大縮小功能ZoomControls控件用法實(shí)例
這篇文章主要介紹了Android編程實(shí)現(xiàn)圖片放大縮小功能ZoomControls控件用法,結(jié)合具體實(shí)例形式分析了Android ZoomControls控件實(shí)現(xiàn)圖片縮放的具體操作方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-09-09詳解Android Libgdx中ScrollPane和Actor事件沖突問題的解決辦法
這篇文章主要介紹了詳解Android Libgdx中ScrollPane和Actor事件沖突問題的解決辦法的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09Android開發(fā)中MotionEvent坐標(biāo)獲取方法分析
這篇文章主要介紹了Android開發(fā)中MotionEvent坐標(biāo)獲取方法,結(jié)合實(shí)例形式分析了MotionEvent獲取坐標(biāo)的相關(guān)函數(shù)使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-02-02Android在自定義類中實(shí)現(xiàn)自定義監(jiān)聽器方式
這篇文章主要介紹了Android在自定義類中實(shí)現(xiàn)自定義監(jiān)聽器方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android AIDL和遠(yuǎn)程Service調(diào)用示例代碼
本文主要介紹Android AIDL和遠(yuǎn)程Service,這里詳細(xì)介紹了相關(guān)知識(shí),并附實(shí)例代碼和實(shí)現(xiàn)效果圖,有興趣的朋友參考下2016-08-08Android控件ToggleButton多狀態(tài)按鈕使用詳解
這篇文章主要為大家詳細(xì)介紹了Android控件ToggleButton多狀態(tài)按鈕的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06