志在指尖
用双手敲打未来

Android View的background和padding

最近在做一个需求,是对im聊天音讯设置气泡背景,之前呢,设计师没有特别遵照一定的设计标准,招致,气泡背景有的是.9的图片,有的是本人用xml画出来的背景。这样在给聊天音讯设置背景的时分呈现了不少的问题。
问题场景回溯:
设置背景,我们常用的Api是setBackgroundResource。最开端思索的比拟简单,每条音讯都只用setBackgroundResource
接着就碰到了第一个问题,有的音讯用的是.9图,有的是xml,整体的效果看起来不是很谐和,也就是音讯的距离有大有小,看起来特别丑。
这里我们想到了一个方法,我们需求让不同的气泡上的文本内容看起来距离差不多,思索的是设置padding,而不是运用background里面的距离。关于每一条音讯,依据它是什么样的气泡,决议设置padding值的参数。Android
大致的代码是
setBgContainerPadding(bgContainer,SystemUtils.dip2px(10),SystemUtils.dip2px(4),SystemUtils.dip2px(18),SystemUtils.dip2px(4));
bgContainer.setBackgroundResource(R.drawable.xxx);//设置背景
然后我就掉入了第二个坑中,发现设置了padding后,效果没变化,一时之间找不到缘由,很是恼火。只好先放弃这块工作,去忙其他的了,不晓得过了多久,灵感来了,想到了调整padding和background的先后次第。
bgContainer.setBackgroundResource(R.drawable.xxx);//设置背景
setBgContainerPadding(bgContainer,SystemUtils.dip2px(10),SystemUtils.dip2px(4),SystemUtils.dip2px(18),SystemUtils.dip2px(4));
一定得是先设置背景,在设置padding值。不能是先设置padding,再设置背景。https://www.jianshu.com/p/4432b19ec6cd这篇文章深化剖析了下缘由,关于这块引荐看这篇文章。
到这里大致的效果就比拟接近了,但是还是有些item的气泡背景包裹的内容距离有问题,觉得是view的复用机制(RecyclerView,ListView的item)在作祟。(此外设置padding值的时分,不要运用view的getPaddingLeft()这样的办法,全部给定详细的数值,从源头上防止复用机制)
于是再次修正代码,代码掩盖各种case,相当于每个itemView都手动设置一遍,padding,气泡背景这块不运用view的复用。
完好的处置气泡的代码如下:
复制代码
privatevoidsetBackground(ViewbgContainer,intposition){
Conversationconversation=conversationList.get(position);
if(position==0){
if(conversation.getIsSend()==Conversation.SEND_RECEIVE_TYPE_SEND){
bgContainer.setBackgroundResource(R.drawable.balloon_outgoing_normal);//marginLeftmarginRight10dp
setBgContainerPadding(bgContainer,SystemUtils.dip2px(10),SystemUtils.dip2px(4),SystemUtils.dip2px(18),SystemUtils.dip2px(4));
}else{
bgContainer.setBackgroundResource(R.drawable.balloon_incoming_normal);
setBgContainerPadding(bgContainer,SystemUtils.dip2px(18),SystemUtils.dip2px(4),SystemUtils.dip2px(10),SystemUtils.dip2px(4));
}
setBgContainerMargin(bgContainer,SystemUtils.dip2px(10),0,SystemUtils.dip2px(10),0);
}
elseif(isDifferentTypePre(position)){
if(conversation.getIsSend()==Conversation.SEND_RECEIVE_TYPE_SEND){
bgContainer.setBackgroundResource(R.drawable.balloon_outgoing_normal);
setBgContainerPadding(bgContainer,SystemUtils.dip2px(10),SystemUtils.dip2px(4),SystemUtils.dip2px(18),SystemUtils.dip2px(4));
}else{
bgContainer.setBackgroundResource(R.drawable.balloon_incoming_normal);
setBgContainerPadding(bgContainer,SystemUtils.dip2px(18),SystemUtils.dip2px(4),SystemUtils.dip2px(10),SystemUtils.dip2px(4));
}
setBgContainerMargin(bgContainer,SystemUtils.dip2px(10),0,SystemUtils.dip2px(10),0);
}else{
lineHeader.setVisibility(View.GONE);
if(conversation.getIsSend()==Conversation.SEND_RECEIVE_TYPE_SEND){
bgContainer.setBackgroundResource(R.drawable.green_msg);
setBgContainerMargin(bgContainer,0,SystemUtils.dip2px(0.5f),SystemUtils.dip2px(17),SystemUtils.dip2px(0.5f));
}else{
bgContainer.setBackgroundResource(R.drawable.white_msg);
setBgContainerMargin(bgContainer,SystemUtils.dip2px(17),SystemUtils.dip2px(0.5f),0,SystemUtils.dip2px(0.5f));
}
setBgContainerPadding(bgContainer,SystemUtils.dip2px(10),SystemUtils.dip2px(4),SystemUtils.dip2px(10),SystemUtils.dip2px(4));
}
}
复制代码
这块的代码,也是调整了良久才完善好,我觉得这块还是很值得总结的。希望对大家有用。
总结:
做这块的工作,碰到了2个问题,设置background跟padding的先后次第,一定得是先设置background再设置padding;第二个是要思索到view的复用,但是关于气泡,特定的背景padding值而言,要用代码的方式禁用掉view的复用效果。最终的音讯聊天气泡效果很让我称心,整个页面的规划效果也很好,仿的特别胜利。

未经允许不得转载:IT技术网站 » Android View的background和padding
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

志在指尖 用双手敲打未来

登录/注册IT技术大全

热门IT技术

C#基础入门   SQL server数据库   系统SEO学习教程   WordPress小技巧   WordPress插件   脚本与源码下载