星空网 > 软件开发 > 操作系统

Android 更改 Toast 的默认位置

Android中Toast的默认位置在屏幕靠近底部的位置,这个默认位置有时候并不合适。比如页面上内容较少时,内容一般集中在屏幕上半部分,用户的注意力也集中在屏幕上半部分,默认位置的Toast用户可能没有注意到。还有可能是默认位置的Toast被用户的手挡住了。实践中感觉将Toast显示在屏幕的中部或中上部会比较好。如何修改Toast的默认位置呢?下面做一个简单的例子来演示一下。

先上截图:

Android 更改 Toast 的默认位置images/loading.gif' data-original="http://images2015.cnblogs.com/blog/343777/201512/343777-20151210114632465-767778224.png" />

Android 更改 Toast 的默认位置

Android 更改 Toast 的默认位置

布局文件activity_toast.

<??><LinearLayout ="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 的默认位置

关键词:Android

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

Shopee卖家计分系统:https://www.goluckyvip.com/news/8048.html
通过卖家自配送API购买配送服务:https://www.goluckyvip.com/news/8049.html
海外仓与亚马逊FBA仓的区别是什么?2019年亚马逊卖家是选择海外仓还是亚马逊FBA仓?:https://www.goluckyvip.com/news/805.html
宁波天盟国际物流有限公司:https://www.goluckyvip.com/news/8050.html
深圳市大洋物流股份有限公司:https://www.goluckyvip.com/news/8051.html
Ninja Outreach:https://www.goluckyvip.com/news/8052.html
海南岛琼海市旅游景点 琼海市的旅游景点:https://www.vstour.cn/a/363189.html
月活用户超20亿!万亿市值巨头对中国商家进一步开闸放流 :https://www.kjdsnews.com/a/1836412.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流