Android應用中Toast的位置如何更改

Android應用中Toast的位置如何更改?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

創(chuàng)新互聯(lián)公司是一家專業(yè)提供犍為企業(yè)網(wǎng)站建設,專注與做網(wǎng)站、網(wǎng)站制作、成都h5網(wǎng)站建設、小程序制作等業(yè)務。10年已為犍為眾多企業(yè)、政府機構(gòu)等服務。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡公司優(yōu)惠進行中。

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

布局文件activity_toast.xml代碼如下:

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<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的位置如何更改問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關知識。

當前名稱:Android應用中Toast的位置如何更改
當前路徑:http://bm7419.com/article4/psdpie.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供GoogleChatGPT、網(wǎng)站內(nèi)鏈、靜態(tài)網(wǎng)站軟件開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)