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

首頁(yè) > 系統(tǒng) > Android > 正文

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

2019-10-23 18:31:07
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

Android中Toast的默認(rèn)位置在屏幕靠近底部的位置,這個(gè)默認(rèn)位置有時(shí)候并不合適。比如頁(yè)面上內(nèi)容較少時(shí),內(nèi)容一般集中在屏幕上半部分,用戶(hù)的注意力也集中在屏幕上半部分,默認(rèn)位置的Toast用戶(hù)可能沒(méi)有注意到。還有可能是默認(rèn)位置的Toast被用戶(hù)的手擋住了。實(shí)踐中感覺(jué)將Toast顯示在屏幕的中部或中上部會(huì)比較好。如何修改Toast的默認(rèn)位置呢?下面做一個(gè)簡(jiǎn)單的例子來(lái)演示一下。

先上截圖:

android,toast,位置

android,toast,位置

android,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="點(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)位置方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到Android開(kāi)發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 凯里市| 桑植县| 马关县| 民权县| 阿鲁科尔沁旗| 吉隆县| 高陵县| 岳西县| 竹山县| 巴林右旗| 白银市| 余姚市| 绥芬河市| 沾益县| 越西县| 千阳县| 星子县| 油尖旺区| 天峨县| 盐边县| 咸阳市| 瑞丽市| 娱乐| 东乡县| 辛集市| 枝江市| 江城| 抚宁县| 大田县| 福建省| 内乡县| 东宁县| 抚顺市| 乌鲁木齐市| 诏安县| 体育| 元江| 和顺县| 吕梁市| 嘉善县| 崇义县|