3

我有一个回收站视图,想更改 textview 文本某些部分的颜色。

这是我的代码:

public class MyAdapter extends RecyclerView.Adapter<MyEarningsAdapter.ViewHolder> {
    public ArrayList<WageItemDataSet> wageItemList;
    private Context context;
    private Spannable spanText;
    private ForegroundColorSpan fcs;
    private RelativeSizeSpan rss;
    public MyAdapter(Context context) {
        wageItemList = new ArrayList<>();
        this.context = context;
        fcs = new ForegroundColorSpan(Color.BLACK);
        rss = new RelativeSizeSpan(0.8f);
    }
 @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_wage_list, parent, false));
    }
    @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            WageItemDataSet item = wageItemList.get(position);
            spanText = new SpannableString("Fare: "+item.totalFare);
            spanText.setSpan(fcs, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            spanText.setSpan(rss, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            holder.tvFareAmount.setText(spanText);

        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            TextView tvFareAmount;
            public ViewHolder(View itemView) {
                super(itemView);
                tvFareAmount = (TextView) itemView.findViewById(R.id.tvFareAmount);

                tvFareAmount.setTypeface(openSansRegular);

            }
        }
}

它不会改变颜色或文本大小。我究竟做错了什么??

4

5 回答 5

5

嗨,我已经实现了同样的事情并且它对我有用。您必须创建一个扩展 ClickableSpan 的类,如下所示:

    public class TextSpan extends ClickableSpan {
      @Override
      public void onClick(View widget) {
      TextView tv = (TextView) widget;
      Spanned s = (Spanned) tv.getText();
      //int start = s.getSpanStart(this);
      //int end = s.getSpanEnd(this);
    }
  }

然后在 onBindViewHolder() 方法中,您必须编写如下代码

     SpannableStringBuilder builder = new SpannableStringBuilder();
     builder.append("yourstring");
     builder.setSpan(new TextSpan(), builder.length() -        
     comment.length(),builder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
     holder.tvFareAmount.setText(content);
  holder.tvFareAmount.setMovementMethod(LinkMovementMethod.getInstance());

我希望这能解决你的问题

于 2017-03-17T06:50:36.497 回答
2

这对我有用 -

StringBuilder captionBuilder = new StringBuilder();
captionBuilder.append(yourString);
SpannableStringBuilder commentBuilder = new SpannableStringBuilder(captionBuilder);
commentBuilder.setSpan(new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.your_color)), start, end, 0);

希望能帮助到你。

于 2017-03-08T09:41:12.520 回答
0

试试这个,

    WageItemDataSet item = wageItemList.get(position);
    String value="Fare: "+item.totalFare;
    Spannable spanText = new SpannableString(value);        
    spanText.setSpan(new ForegroundColorSpan(Color.BLUE), 5, 13, 
    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    textview.setText(spanText);
于 2017-03-08T09:27:49.453 回答
0

使用下面来实现您的目标...

public static void spanText(Context context, AppCompatAutoCompleteTextView editText, String replaceString) {
    String event = editText.getHint().toString();
    int startPos = event.indexOf(replaceString);
    int endPos = startPos + replaceString.length();

    Spannable spanText = Spannable.Factory.getInstance().newSpannable(editText.getHint());
    spanText.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.dark_yellow)), startPos, endPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    editText.setHint(spanText);
}
于 2017-03-08T10:23:57.823 回答
0

尝试 .append() 而不是 .settext()

holder.tvFareAmount.append(spanText)
于 2017-03-08T15:41:34.360 回答