您的位置:首页 > 其它

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

2015-04-03 19:37 176 查看
Toaster是常用的控件,toast的显示位置是默认在靠近底部的,但有时候在用户进行输入的使用,软键盘打开,会阻挡着toaster的显示,所以通过改变toaster的显示位置位于标题栏的底部,这样更能用户有效的看到。而且可以通过设置不同颜色的toaster来表示不同的含义。魅族就是这样做的。具体代码:
[code]Toasttoast;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toast=newToast(this);
toast.setDuration(Toast.LENGTH_SHORT);
Viewtoastview=LayoutInflater.from(this).inflate(
R.layout.toaster_view,null);
TextViewtext=(TextView)toastview.findViewById(R.id.ivory_toast_text);
LayoutParamsparams=text.getLayoutParams();
params.width=getWindowManager().getDefaultDisplay().getWidth();;
text.setLayoutParams(params);
toast.setView(toastview);
toast.setGravity(Gravity.TOP,0,dip2px(this,56));
toast.show();
}
publicstaticintdip2px(Contextcontext,floatdipValue){
floatscale=context.getResources().getDisplayMetrics().density;
return(int)(scale*dipValue+0.5f);
}
布局文件:

[code]<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns: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>

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: