----前言要写在最前面
“快啵”项目2.0版本开发已告一段落,之前的笔记都在“为知”,新年决定,把滴水汇入江河才不会干涸,目前正在学习项目里的插件化开发,我会逐步把笔记转入线上,谢谢支持!
----项目介绍
“快啵”致力于同城婚恋交友,是一款集合于即时通讯,网络数据处理,各种支付方式融合的app,同事们各司其职,虽历尽艰辛,终拨云见日,一年时间,团队磨合,技术积累,正如我们挂在嘴边的一句话“交朋友,学技术,赚money”!
-----此博文解决的问题:自定义Toast的编写
给予程序猿最好的礼物不是代码,而是有注释的代码!
/**
* 打招呼的自定义布局
* @param context 上下文
* @param msg toast弹出内容
* @return LinearLayout
*/
private static LinearLayout getSayHiLayout(Context context, String msg)
{
//GradientDrawable支持使用渐变色来绘制图形,通常可以用作Button或是背景图形
GradientDrawable backgroundDrawable = new GradientDrawable();
backgroundDrawable.setColor(Color.parseColor("#99000000"));//设置填充背景颜色
backgroundDrawable.setCornerRadius((float) DPUtils.dp2px(context,10.0F));//设置四角半径
//整体布局是LinearLayout
LinearLayout defaultLayout = new LinearLayout(context);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
defaultLayout.setGravity(Gravity.CENTER);
defaultLayout.setOrientation(LinearLayout.HORIZONTAL);
int px = DPUtils.dp2px(context,30.0F);//左右边距
int px2 = DPUtils.dp2px(context,20.0F);//上下边距
defaultLayout.setPadding(px,px2,px,px2);//设置边距
defaultLayout.setLayoutParams(lp);
defaultLayout.setMinimumWidth(DPUtils.dp2px(context,100.0F));//最小宽度
defaultLayout.setMinimumHeight(DPUtils.dp2px(context,100.0F));//最小高度
defaultLayout.setBackgroundDrawable(backgroundDrawable);//设置背景
//布局里的TextView,“打招呼”的文字位置,大小,颜色等
TextView textView = new TextView(context);
LinearLayout.LayoutParams tvlp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
tvlp.gravity = Gravity.CENTER;
textView.setTextColor(Color.WHITE);
textView.setTextSize(16.0F);
textView.setText(msg);
textView.setLayoutParams(tvlp);
defaultLayout.addView(textView);
//心动图片,展示在文字右侧,这里父布局LinearLayout设置了水平朝向
ImageView imageView = new ImageView(context);
imageView.setLayoutParams(tvlp);
tvlp.setMargins(DPUtils.dp2px(context,2.0F),DPUtils.dp2px(context,5.0F),DPUtils.dp2px(context,2.0F),DPUtils.dp2px(context,5.0F));
imageView.setLayoutParams(tvlp);
//加载Assets目录里的资源图片
Resources res = context.getResources();
try
{
BitmapDrawable e = new BitmapDrawable(res,res.getAssets().open("hello.png"));
imageView.setImageBitmap(e.getBitmap());
}
catch (IOException e1)
{
e1.printStackTrace();
}
defaultLayout.addView(imageView);
return defaultLayout;
}
----效果展示