此篇博客是記錄一些TextView開發上一些少用的開發功能項.目前開發記錄以下:java
android:maxLines="2"
android:ellipsize="end"
/** * Gets the {@link android.text.Layout} that is currently being used to display the text. * This value can be null if the text or width has recently changed. * @return The Layout that is currently being used to display the text. */ public final Layout getLayout() { return mLayout; }
/** * Returns the number of characters to be ellipsized away, or 0 if * no ellipsis is to take place. */ public abstract int getEllipsisCount(int line);
//獲取省略的字符數,0表示沒省略 int ellipsisCountholder = textView.getLayout().getEllipsisCount(holder.content.getLineCount()-1);
holder.content.post(new Runnable() { @Override public void run() { //獲取省略的字符數,0表示沒和省略 int ellipsisCountholder = holder.content.getLayout().getEllipsisCount(holder.content.getLineCount()-1); if (ellipsisCountholder != 0){ holder.telescopicContent.setText("全文"); holder.telescopicContent.setVisibility(View.VISIBLE); } } });
viewHolder.telescopicContent.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (viewHolder.telescopicContent.getText().equals("全文")){ viewHolder.telescopicContent.setText("收起"); viewHolder.content.setMaxLines(50); }else { viewHolder.telescopicContent.setText("全文"); viewHolder.content.setMaxLines(4); } } });
SpannableString spannableString = new SpannableString("今每天氣不錯"); spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#FF0000")), 2, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); tv5.setText(spannableString);
setSpan方法有四個參數,android
第一個參數:ForegroundColorSpan是爲文本設置前景色,也就是文字顏色。若是要爲文字添加背景顏色,可替換爲BackgroundColorSpan。ide
第二個參數:文本顏色改變的起始位置post
第三個參數:文本顏色改變的結束位置。spa
最後一個參數爲布爾型,能夠傳入如下四種:code