自定义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>
相关文章推荐
- 自定义底部显示并没有标题栏的Dialog
- Android自定义标题栏:显示网页加载进度
- Android--自定义标题栏之显示网页加载进度
- android自定义PopWindow底部显示
- Android 应用程序窗体显示状态操作(显示隐藏标题栏或自定义窗体大小)
- Android自定义标题栏:显示网页加载进度
- Android--自定义标题栏之显示网页加载进度
- Android在activity页面中让标题栏全屏显示自定义颜色
- Android底部菜单栏、Android沉浸式状态栏(顶部状态栏修改颜色)、自定义标题栏
- 顶部固定显示;某模块的标题栏在滚动时固定显示;到底部时显示底部banner;回到顶部。4个效果
- 隐藏标题栏和底部操作栏,可上下滑动显示
- Android--自定义标题栏之显示网页加载进度
- 自定义Activity标题栏(Title bar)和窗体显示状态操作(requestWindowFeature()的应用)
- 自定义Activity标题栏(Title bar)和窗体显示状态操作(requestWindowFeature()的应用)
- Android自定义底部显示对话框
- 自定义一个在底部显示的dialog
- Android 应用程序窗体显示状态操作(显示隐藏标题栏或自定义窗体大小)
- 自定义Activity标题栏(Title bar)和窗体显示状态操作(requestWindowFeature()的应用)
- Android自定义系列(1)——自定义view 仿微信viewPage底部显示图标(整理来自慕课网)
- 自定义dialog显示在底部