自定义Toaster,显示在标题栏底部

    Toaster是常用的控件,toast的显示位置是默认在靠近底部的,但有时候在用户进行输入的使用,软键盘打开,会阻挡着toaster的显示,所以通过改变toaster的显示位置位于标题栏的底部,这样更能用户有效的看到。而且可以通过设置不同颜色的toaster来表示不同的含义。魅族就是这样做的。

具体代码:
    
    
Toast toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toast= new Toast(this);
toast.setDuration(Toast.LENGTH_SHORT);
View toastview = LayoutInflater.from(this).inflate(
R.layout.toaster_view, null);
TextView text = (TextView) toastview.findViewById(R.id.ivory_toast_text);
LayoutParams params = text.getLayoutParams();
params.width = getWindowManager().getDefaultDisplay().getWidth();;
text.setLayoutParams(params);
toast.setView(toastview);
toast.setGravity(Gravity.TOP, 0, dip2px(this,56));
toast.show();
}
public static int dip2px(Context context,float dipValue){
float scale=context.getResources().getDisplayMetrics().density;
return (int) (scale*dipValue+0.5f);
}
布局文件:

     
     
<?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" >
 
<TextView
android:id="@+id/ivory_toast_text"
android:background="#d8FFFFFF"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#444444"
android:gravity="center"
android:textSize="14sp"
android:text="你没有权限" />
<View
android:layout_width="fill_parent"
android:layout_height="1px"
android:background="#dddddd"/>
</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值