国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 更改 Toast 的默認位置方法

2019-12-12 03:03:54
字體:
來源:轉載
供稿:網友

Android中Toast的默認位置在屏幕靠近底部的位置,這個默認位置有時候并不合適。比如頁面上內容較少時,內容一般集中在屏幕上半部分,用戶的注意力也集中在屏幕上半部分,默認位置的Toast用戶可能沒有注意到。還有可能是默認位置的Toast被用戶的手擋住了。實踐中感覺將Toast顯示在屏幕的中部或中上部會比較好。如何修改Toast的默認位置呢?下面做一個簡單的例子來演示一下。

先上截圖:

布局文件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="點擊顯示默認位置的Toast" />  <Button    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:onClick="onClickCenterToast"    android:text="點擊顯示居中位置的Toast" />  <Button    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:onClick="onClickTopToast"    android:text="點擊顯示居中上部位置的Toast" /></LinearLayout>

后臺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, "默認位置的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);    // 這里給了一個1/4屏幕高度的y軸偏移量    toast.setGravity(Gravity.TOP, 0, height / 4);    toast.show();  }}

以上這篇Android 更改 Toast 的默認位置方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 台北市| 泸州市| 海宁市| 娄烦县| 洪雅县| 永嘉县| 天水市| 乐平市| 淅川县| 汉寿县| 许昌县| 伊川县| 万宁市| 云霄县| 伊金霍洛旗| 图木舒克市| 阜南县| 拜城县| 曲沃县| 扶沟县| 衡南县| 仲巴县| 长岭县| 安义县| 淮滨县| 武穴市| 蒙阴县| 曲阜市| 巴林左旗| 永福县| 新津县| 商河县| 金塔县| 甘德县| 凤阳县| 革吉县| 桂东县| 中牟县| 澎湖县| 甘德县| 封开县|