一、给textview设置颜色为#fffff
有时候我们会从服务器获得数据,但是后台给的服务器的值是比如:#B373FB
此时要想给TextView设置文字直接设置textview.setTextColor();是不行的。此时我们可以这样
String str = floorcolor; int id = Color.parseColor(str);//#ffffff设置成color对象 tv_select1.setTextColor(id);
tv_select1为TextView,floorcolor比如:"#ffffff"
二、TextView的跑马灯效果,这个很简单
为了解决TextView不会在失去焦点的时候不滚动的问题,我自定义一个TextView,重写isFocused方法设置为true
public class MarqueeTextView extends TextView { public MarqueeTextView(Context context) { super(context); } public MarqueeTextView(Context context, AttributeSet attrs) { super(context, attrs); } public MarqueeTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public boolean isFocused() { return true; } }跑马灯布局如下
<com.spark.huabang.view.MarqueeTextView android:id="@+id/get_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="35dp" android:layout_marginLeft="10dp" android:ellipsize="marquee" android:focusable="true" android:marqueeRepeatLimit="marquee_forever" android:singleLine="true" android:text="中奖名单:" android:textColor="@android:color/white" android:textSize="16sp"> </com.spark.huabang.view.MarqueeTextView>
三、现在跑马灯的TextView的数据,我假设有三段字符串构成:
Textview.setText(s);
s=s1+s2+s3;
此时,要想设置第二段字符串的颜色为红色,可以使用spannableStringBuilder的方法:
如果对首先用s+=s1;然后用
SpannableStringBuilder style = new SpannableStringBuilder(s);
style.setSpan(new ForegroundColorSpan(Color.RED), 0, s2.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
最后使用s=s+s2+s3;TextView.setText(s);运行后会发现没反应,此时应该这么做才是正确的:
SpannableStringBuilder style = new SpannableStringBuilder(s);
style.setSpan(new ForegroundColorSpan(Color.RED), s.length()-s2.length,
s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView.setText(style);
此时就可以设置完成了