最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android 更改 Toast 的默認(rèn)位置方法

 更新時(shí)間:2017年04月18日 10:51:04   投稿:jingxian  
下面小編就為大家?guī)硪黄狝ndroid 更改 Toast 的默認(rèn)位置方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

Android中Toast的默認(rèn)位置在屏幕靠近底部的位置,這個(gè)默認(rèn)位置有時(shí)候并不合適。比如頁面上內(nèi)容較少時(shí),內(nèi)容一般集中在屏幕上半部分,用戶的注意力也集中在屏幕上半部分,默認(rèn)位置的Toast用戶可能沒有注意到。還有可能是默認(rèn)位置的Toast被用戶的手擋住了。實(shí)踐中感覺將Toast顯示在屏幕的中部或中上部會比較好。如何修改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>

后臺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)文章

最新評論

依安县| 萍乡市| 桐庐县| 江华| 阳信县| 营山县| 鹿泉市| 固安县| 宁晋县| 汽车| 白朗县| 石棉县| 凭祥市| 张家港市| 大英县| 调兵山市| 阿拉善右旗| 鹤庆县| 弥渡县| 东阳市| 门头沟区| 兴海县| 南华县| 滁州市| 安岳县| 镇原县| 北宁市| 垫江县| 丹棱县| 仲巴县| 安乡县| 中方县| 凤凰县| 澄迈县| 宁明县| 白玉县| 海南省| 佛山市| 嘉鱼县| 县级市| 桃源县|