像微信朋友圈点赞功能如:张三、李四、王五、这种格式
最早做法是在layout中创建一个父类容器如linearlayout然后在创建一个子layout,在代码中 通过for循环addView添加到父类容器当中,虽然可以实现效果但是这样无形中会增加很多view控件。
通过SpannableStringBuilder只通过一个textview就可以实现上面的效果。
InvitationLike userLike; SpannableStringBuilder builder = new SpannableStringBuilder();SpannableString spannableString;for(int i=0;i<userLikeList.size();i++){userLike=userLikeList.get(i); String strUserName="、"+userLike.getUsername(); spannableString = new SpannableString(strUserName);spannableString.setSpan(likeTextViewCliceSpan(userLike), 0, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);builder.append(spannableString);} txtUserName.setText(builder.subSequence(1, builder.length()));txtUserName.setMovementMethod(LinkMovementMethod.getInstance());
likeTextViewCliceSpan是事件方法
private ClickableSpan likeTextViewCliceSpan(final InvitationLike user_like){ClickableSpan clickableSpan=new ClickableSpan() {@Overridepublic void onClick(View widget) {StartPace(user_like.getUtid(),user_like.getUid(),user_like.getUsername());}@Overridepublic void updateDrawState(@NonNull TextPaint ds) {super.updateDrawState(ds);//根据自己情况设置相应字体颜色ds.setColor(mContext.getResources().getColor(R.color.broadcast_list_name));ds.setUnderlineText(false);ds.clearShadowLayer();} };return clickableSpan;}
同理微信中的回复功能也一样实现通过一个textview实现,如果实用多个textview就很难保证字体对齐方式。
微信回复格式
李四回复张三:谢谢
张三:不错啊
这个样式父类必须创建一人上layout,因为有多行,每行是一个textview控件,textviewClickSpan也是事件方法和上面基本一样这里就是粘贴了
private void userCommentList(final int index,LinearLayout linearLike, final List<NewCommentModel> commentList){linearLike.removeAllViews();NewCommentModel commentModel;for(int i=0;i<commentList.size();i++){commentModel=commentList.get(i); View view=LayoutInflater.from(mContext).inflate(R.layout.view_userlike_txtname,new LinearLayout(mContext));final CustomTextView txt_commentContent=(CustomTextView)view.findViewById(R.id.txt_username); ForegroundColorSpan replySpan = new ForegroundColorSpan(mContext.getResources().getColor(R.color.login_font));ForegroundColorSpan contentSpan = new ForegroundColorSpan(mContext.getResources().getColor(R.color.login_font));SpannableStringBuilder builder = new SpannableStringBuilder();SpannableString fromSpannable,spannableReply,toSpannable,contentSpannable;if(TextUtils.isEmpty(commentModel.getTousername())){ fromSpannable = new SpannableString(commentModel.getFromusername()+": ");fromSpannable.setSpan(textviewClickSpan(false,commentModel), 0, fromSpannable.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);builder.append(fromSpannable);}else{ fromSpannable = new SpannableString(commentModel.getFromusername());fromSpannable.setSpan(textviewClickSpan(false,commentModel), 0, fromSpannable.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);builder.append(fromSpannable);spannableReply=new SpannableString(mContext.getResources().getString(R.string.etxt_reply_hit));spannableReply.setSpan(replySpan, 0, spannableReply.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);builder.append(spannableReply);toSpannable = new SpannableString(commentModel.getTousername()+": ");toSpannable.setSpan(textviewClickSpan(true,commentModel), 0, toSpannable.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);builder.append(toSpannable); }contentSpannable = new SpannableString(commentModel.getContent());contentSpannable.setSpan(contentSpan, 0, contentSpannable.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);builder.append(contentSpannable);txt_commentContent.setText(builder);txt_commentContent.setMovementMethod(LinkMovementMethod.getInstance());linearLike.addView(view);} }