Android Open Source - 50AndroidHacks Format Text View






From Project

Back to project page 50AndroidHacks.

License

The source code is released under:

Apache License

If you think the Android project 50AndroidHacks listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.github.yftx.AndroidHacks.sectionTipsAndTricks;
//  w ww  .ja  v a 2s  .  c  om
import android.app.Activity;
import android.content.res.ColorStateList;
import android.content.res.XmlResourceParser;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.*;
import android.widget.TextView;
import com.github.yftx.AndroidHacks.R;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;

/**
 * User: Liuzl
 * Date: 13-11-27
 * <p/>
 * ???????
 * ???????
 * ???????
 * ?????????????
 * ??????????(?????????)
 * ??????????(????????????????????)
 */
public class FormatTextView extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.format_textview_activity);
        initHtml();
        initItalic();
    }

    private void initItalic() {
        //???? SpannableString??
        SpannableString msp = new SpannableString("??????????????????????????????????????x1x2?????????????X?????/bot");

        //????(default,default-bold,monospace,serif,sans-serif)
        msp.setSpan(new TypefaceSpan("monospace"), 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        msp.setSpan(new TypefaceSpan("serif"), 2, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        //????????????,??????????
        msp.setSpan(new AbsoluteSizeSpan(20), 4, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        msp.setSpan(new AbsoluteSizeSpan(20, true), 6, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  //???????boolean dip????true????????????????????dip???????????????

        //???????????,?????????? ???????????????????
        msp.setSpan(new RelativeSizeSpan(0.5f), 8, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  //0.5f??????????????
        msp.setSpan(new RelativeSizeSpan(2.0f), 10, 12, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  //2.0f?????????????

        //????????
        msp.setSpan(new ForegroundColorSpan(Color.MAGENTA), 12, 15, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  //??????????

        //???????
        msp.setSpan(new BackgroundColorSpan(Color.CYAN), 15, 18, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  //??????????

        //???????????????????
        msp.setSpan(new StyleSpan(android.graphics.Typeface.NORMAL), 18, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  //??
        msp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 20, 22, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  //??
        msp.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 22, 24, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  //??
        msp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), 24, 27, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  //???

        //?????
        msp.setSpan(new UnderlineSpan(), 27, 30, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        //?????
        msp.setSpan(new StrikethroughSpan(), 30, 33, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        //?????
        msp.setSpan(new SubscriptSpan(), 34, 35, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);     //??
        msp.setSpan(new SuperscriptSpan(), 36, 37, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);   //??

        //??????????setMovementMethod????????
        msp.setSpan(new URLSpan("tel:4155551212"), 37, 39, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);     //???
        msp.setSpan(new URLSpan("mailto:webmaster@google.com"), 39, 41, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);     //??
        msp.setSpan(new URLSpan("http://www.baidu.com"), 41, 43, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);     //??
        msp.setSpan(new URLSpan("sms:4155551212"), 43, 45, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);     //??   ??sms:??smsto:
        msp.setSpan(new URLSpan("mms:4155551212"), 45, 47, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);     //??   ??mms:??mmsto:
        msp.setSpan(new URLSpan("geo:38.899533,-77.036476"), 47, 49, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);     //??

        //???????????,?????????? ??????????????????
        msp.setSpan(new ScaleXSpan(2.0f), 49, 51, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //2.0f????????????????X?????????????????????????

        //???????????????????????????????????????
        ColorStateList csllink = null;
        ColorStateList csl = null;
        XmlResourceParser xppcolor = getResources().getXml(R.color.linkcolor);
        try {
            csl = ColorStateList.createFromXml(getResources(), xppcolor);
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        XmlResourceParser xpplinkcolor = getResources().getXml(R.color.linkcolor);
        try {
            csllink = ColorStateList.createFromXml(getResources(), xpplinkcolor);
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        msp.setSpan(new TextAppearanceSpan("monospace", android.graphics.Typeface.BOLD_ITALIC, 30, csl, csllink),
                51, 53, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        //????????
        msp.setSpan(new BulletSpan(android.text.style.BulletSpan.STANDARD_GAP_WIDTH, Color.GREEN),
                0, msp.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //????????????????????????????????????????

        //????
        Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        msp.setSpan(new ImageSpan(drawable), 53, 57, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        TextView tv = (TextView) findViewById(R.id.html_italic);
        tv.setText(msp);
        tv.setMovementMethod(LinkMovementMethod.getInstance());
    }

    private void initHtml() {
        TextView tv = (TextView) findViewById(R.id.html);
        String text = "Visit <a href=\"http://baidu.com/\">Baidu</a>";
        tv.setText(Html.fromHtml(text));
        tv.setMovementMethod(LinkMovementMethod.getInstance());
    }
}




Java Source Code List

com.github.yftx.AndroidHacks.MainActivity.java
com.github.yftx.AndroidHacks.sectionAdapter.AdapterActivity.java
com.github.yftx.AndroidHacks.sectionAdapter.SampleAdapter.java
com.github.yftx.AndroidHacks.sectionAnimation.KenBurnsEffect.java
com.github.yftx.AndroidHacks.sectionAnimation.TextAndImageAnimation.java
com.github.yftx.AndroidHacks.sectionLayout.CanvasActivity.java
com.github.yftx.AndroidHacks.sectionLayout.WeightActivity.java
com.github.yftx.AndroidHacks.sectionLayout.customview.Cascade.java
com.github.yftx.AndroidHacks.sectionLayout.customview.CustomCard.java
com.github.yftx.AndroidHacks.sectionPatterns.broadcast.BroadcastReceiverActivity.java
com.github.yftx.AndroidHacks.sectionPatterns.broadcast.MyService.java
com.github.yftx.AndroidHacks.sectionPatterns.mvp.model.IOpenStatus.java
com.github.yftx.AndroidHacks.sectionPatterns.mvp.model.impl.OpenStatus.java
com.github.yftx.AndroidHacks.sectionPatterns.mvp.presenter.SplashPresenter.java
com.github.yftx.AndroidHacks.sectionPatterns.mvp.view.ISplashView.java
com.github.yftx.AndroidHacks.sectionPatterns.mvp.view.impl.SplashActivity.java
com.github.yftx.AndroidHacks.sectionTipsAndTricks.FormatTextView.java
com.github.yftx.AndroidHacks.sectionTipsAndTricks.GlowTextView.java
com.github.yftx.AndroidHacks.sectionTipsAndTricks.LEDTextView.java
com.github.yftx.AndroidHacks.sectionTipsAndTricks.NoBgPhoneWindow.java
com.github.yftx.AndroidHacks.sectionTipsAndTricks.ScreenOrientation.java
com.github.yftx.AndroidHacks.sectionTipsAndTricks.TestFragmentAdapter.java
com.github.yftx.AndroidHacks.sectionTipsAndTricks.TestFragment.java
com.github.yftx.AndroidHacks.sectionTipsAndTricks.ToastPostion.java