Getting started with Android: textview

Source: Internet
Author: User
I. Set the color of Some fonts

Pass

Spannablestringbuilder style = new spannablestringbuilder (charsequence CS );

Style. setspan (New foregroundcolorspan (color), begin, end, spannable. span_exclusive_exclusive );

TV. settext (style );

Settings. Due to the complexity of this method, I implemented a helper class to quickly implement the color of Some fonts;

final class TextViewUtil{public static CharSequence setTextColor(CharSequence str,int begin,int end,int color){SpannableStringBuilder style = new SpannableStringBuilder(str);style.setSpan(new ForegroundColorSpan(color), begin, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);return style;} }

Test code:

package org.xiazdong;import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.text.Spannable;import android.text.SpannableStringBuilder;import android.text.style.ForegroundColorSpan;import android.widget.TextView;public class MainActivity extends Activity {private TextView tv;@Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        tv = (TextView)findViewById(R.id.tv);        String str = "hello,xiazdong";        CharSequence cq = TextViewUtil.setTextColor(str, 0, 2, Color.RED);        cq = TextViewUtil.setTextColor(cq, 2, 5, Color.GREEN);        tv.setText(cq);    }}

Effect:

Ii. Achieve the running horse light effect (rolling subtitles)

Set the following in <textview> of the main. xml layout file:

android:singleLine="true"       android:focusable="true"       android:ellipsize="marquee"       android:marqueeRepeatLimit="marquee_forever"       android:focusableInTouchMode="true"

Effect:

3. Implement hyperchain

Set <textview> as follows:

Android: autolink = "all", if the string in textview is "Hello, xiazdong's blog: http://blog.csdn.net/xiazdong, phone: 12345678901"

The system automatically recognizes http://blog.csdn.net/xiazdongand 12345678901;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.