android 实现TextView多样式

最近在修改之前的代码发现了有个页面布局如下

android 实现TextView多样式

 这样的话就会导致字体增多就很难看

原始xml布局如下 只截取有关部分

 <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/ten"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/upload_from"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="4sp"
                android:text=""
                android:textColor="@color/blue"
                android:textSize="12sp"
                android:textStyle="bold" />


            <TextView
                android:id="@+id/reply"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="4dp"
                android:text=""
                android:textColor="@color/defcolor2"
                android:textSize="12sp"
                android:visibility="gone"
                 />

            <TextView
                android:id="@+id/upload_to"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="4sp"
                android:text=""
                android:textColor="@color/blue"
                android:textSize="12sp"
                android:textStyle="bold"
                android:visibility="gone"
                />

            <TextView
                android:id="@+id/upload_board"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="10dp"
                android:gravity="center|left"
                android:text=""
                android:textColor="@color/defcolor0"
                android:textSize="12sp" />

        </LinearLayout>

相关逻辑判断

    private void getbusinesslist() {
        adapter = new LCommonAdapter<GetUploadingMessageBean.DataBean>(mContext, R.layout.item_message_board_info) {
            @Override
            protected void convert(LViewHolder viewHolder, GetUploadingMessageBean.DataBean item, int position) {
                viewHolder.setText(R.id.upload_board, item.getContent());
                viewHolder.setText(R.id.upload_from, item.getUserName() + ":");
                viewHolder.setVisible(R.id.reply, false);
                viewHolder.setVisible(R.id.upload_to, false);
                if (!TextUtils.isEmpty(item.getReplyName())) {
                    viewHolder.setVisible(R.id.reply, true);
                    viewHolder.setText(R.id.reply, "回复");
                    viewHolder.setText(R.id.upload_to, item.getReplyName()+":");
                    viewHolder.setVisible(R.id.upload_to, true);
                }


                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
                Date d1 = new Date(item.getCreateTime());
                String t1 = format.format(d1);
                viewHolder.setText(R.id.upload_time, t1);

                viewHolder.setOnClickListener(R.id.reply_msg, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        uploadto = item.getUserName();
                        et_message.setHint("回复:" + uploadto);
                        et_message.setFocusable(true);
                        et_message.setFocusableInTouchMode(true);
                        et_message.requestFocus();
                        //弹出软键盘
                        InputMethodManager inputManager = (InputMethodManager) et_message.getContext()
                                .getSystemService(Context.INPUT_METHOD_SERVICE);
                        inputManager.showSoftInput(et_message, 0);
                    }
                });
            }
        };
        adapter.clear();
        adapter.notifyDataSetChanged();
        live_message.setAdapter(adapter);
        live_message.setFocusable(false);
    }

代码很繁琐而且布局也不对

 

解决方案:

Xml文件修改为

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/ten"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/content"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="10dp"
                android:gravity="center|left"
                android:text=""
                android:textColor="@color/defcolor0"
                android:textSize="12sp" />

        </LinearLayout>

这里只使用一个TextView

逻辑判断

private void getbusinesslist() {
    adapter = new LCommonAdapter<GetUploadingMessageBean.DataBean>(mContext, R.layout.item_message_board_info_test) {
        @Override
        protected void convert(LViewHolder viewHolder, GetUploadingMessageBean.DataBean item, int position) {
            String content=item.getContent();
            String from=item.getUserName();
            String to=item.getReplyName();
            String replymsg=from+" 回复 :"+to+" "+content;
            int countfrom=from.length();
            int countto=from.length();
            String noreply=from+" : "+content;

            if (!TextUtils.isEmpty(to)){
                SpannableString spannableString=new SpannableString(replymsg);
//              ForegroundColorSpan span=new ForegroundColorSpan(mContext.getResources().getColor(R.color.blue));
// spannableString.setSpan(span,0, countfrom, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
// spannableString.setSpan(span,countfrom+5,countfrom+5+countto,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
                spannableString.setSpan(CharacterStyle.wrap(new ForegroundColorSpan(mContext.getResources().getColor(R.color.blue))),0, countfrom, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
                spannableString.setSpan(CharacterStyle.wrap(new ForegroundColorSpan(mContext.getResources().getColor(R.color.blue))),countfrom+5,countfrom+5+countto,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
                viewHolder.setText(R.id.content,spannableString);
            }else {
                SpannableString spannableString=new SpannableString(noreply);
                ForegroundColorSpan span=new ForegroundColorSpan(mContext.getResources().getColor(R.color.blue));
                spannableString.setSpan(span,0, countfrom, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
                viewHolder.setText(R.id.content,spannableString);
            }
            
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
            Date d1 = new Date(item.getCreateTime());
            String t1 = format.format(d1);
            viewHolder.setText(R.id.upload_time, t1);

            viewHolder.setOnClickListener(R.id.reply_msg, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    uploadto = item.getUserName();
                    et_message.setHint("回复:" + uploadto);
                    et_message.setFocusable(true);
                    et_message.setFocusableInTouchMode(true);
                    et_message.requestFocus();
                    //弹出软键盘
                    InputMethodManager inputManager = (InputMethodManager) et_message.getContext()
                            .getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputManager.showSoftInput(et_message, 0);
                }
            });
        }
    };
    adapter.clear();
    adapter.notifyDataSetChanged();
    live_message.setAdapter(adapter);
    live_message.setFocusable(false);
}

使用SpannableStringBuilder 即可操作同一个TextView中的值实现不同样式和颜色,这里使用方法就不举例了

需要注意的是也是踩坑的是

在同一个spannablestring中同时使用两次

.span方法时会有一个方法不起效,即使你确定了两次方法的起始和终止位置不同但也没用

如上文

spannableString.setSpan(CharacterStyle.wrap(new ForegroundColorSpan(mContext.getResources().getColor(R.color.blue))),0, countfrom, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

我使用了

CharacterStyle.wrap()的方法进行带入

详细解释这里有

https://blog.csdn.net/gxp1182893781/article/details/76916796

最终实现效果如下所示:可以看到就很好了不会出现布局错乱的问题了

 

 

android 实现TextView多样式

 

上一篇:CategoricalNF


下一篇:模拟退火算法求解最优化问题