1002-content-style-text

描述设置一个空格设置不同风格的文字

1.通过标签在资源文件里设置

 <string name="styled_text">Plain, <b>bold</b>, <i>italic</i>, <b><i>bold-italic</i></b></string>

2.在代码里直接对文字进行处理

spannableStringBuilder 用法详解:

 SpannableString ss = new SpannableString("红色打电话斜体删除线绿色下划线图片:."); 
         //用颜色标记文本
         ss.setSpan(new ForegroundColorSpan(Color.RED), 0, 2, 
                 //setSpan时需要指定的 flag,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE(前后都不包括).
                 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
         //用超链接标记文本
         ss.setSpan(new URLSpan("tel:4155551212"), 2, 5, 
                 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
         //用样式标记文本(斜体)
         ss.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 5, 7, 
                 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
         //用删除线标记文本
         ss.setSpan(new StrikethroughSpan(), 7, 10, 
                 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
         //用下划线标记文本
         ss.setSpan(new UnderlineSpan(), 10, 16, 
                 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
         //用颜色标记
         ss.setSpan(new ForegroundColorSpan(Color.GREEN), 10, 13, 
                 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
         //获取Drawable资源
         Drawable d = getResources().getDrawable(R.drawable.icon); 
         d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
         //创建ImageSpan
         ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
         //用ImageSpan替换文本
         ss.setSpan(span, 18, 19, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); 
         txtInfo.setText(ss);
         txtInfo.setMovementMethod(LinkMovementMethod.getInstance()); //实现文本的滚动  

通常用于显示文字,但有时候也需要在文字中夹杂一些图片,比如QQ中就可以使用表情图片,又比如需要的文字高亮显示等等,如何在android中也做到这样呢?
记得android中有个android.text包,这里提供了对文本的强大的处理功能。
添加图片主要用SpannableString和ImageSpan类:
 
     Drawable drawable = getResources().getDrawable(id); 
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); 
        //需要处理的文本,[smile]是需要被替代的文本 
        SpannableString spannable = new SpannableString(getText().toString()+"[smile]"); 
        //要让图片替代指定的文字就要用ImageSpan 
        ImageSpan span = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE); 
        //开始替换,注意第2和第3个参数表示从哪里开始替换到哪里替换结束(start和end) 
       //最后一个参数类似数学中的集合,[5,12)表示从5到12,包括5但不包括12 
        spannable.setSpan(span, getText().length(),getText().length()+"[smile]".length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);   
        setText(spannable); 
 
将需要的文字高亮显示:
 
 
 
public void highlight(int start,int end){ 
        SpannableStringBuilder spannable=new SpannableStringBuilder(getText().toString());//用于可变字符串 
        ForegroundColorSpan span=new ForegroundColorSpan(Color.RED); 
        spannable.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
        setText(spannable); 
    } 
  
加下划线:
 
 
 
public void underline(int start,int end){ 
        SpannableStringBuilder spannable=new SpannableStringBuilder(getText().toString()); 
        CharacterStyle span=new UnderlineSpan(); 
        spannable.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
        setText(spannable); 
    } 
  
组合运用:
 
 
 
SpannableStringBuilder spannable=new SpannableStringBuilder(getText().toString()); 
        CharacterStyle span_1=new StyleSpan(android.graphics.Typeface.ITALIC); 
        CharacterStyle span_2=new ForegroundColorSpan(Color.RED); 
        spannable.setSpan(span_1, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
        spannable.setSpan(span_2, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
        setText(spannable); 
  
案例:带有\n换行符的字符串都可以用此方法显示2种颜色
 
 
 
    /**
     * 带有\n换行符的字符串都可以用此方法显示2种颜色
     * @param text
     * @param color1
     * @param color2
     * @return
     */ 
    public SpannableStringBuilder highlight(String text,int color1,int color2,int fontSize){ 
        SpannableStringBuilder spannable=new SpannableStringBuilder(text);//用于可变字符串 
        CharacterStyle span_0=null,span_1=null,span_2; 
        int end=text.indexOf("\n"); 
        if(end==-1){//如果没有换行符就使用第一种颜色显示 
            span_0=new ForegroundColorSpan(color1); 
            spannable.setSpan(span_0, 0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
        }else{ 
            span_0=new ForegroundColorSpan(color1); 
            span_1=new ForegroundColorSpan(color2); 
            spannable.setSpan(span_0, 0, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
            spannable.setSpan(span_1, end+1, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
             
            span_2=new AbsoluteSizeSpan(fontSize);//字体大小 
            spannable.setSpan(span_2, end+1, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
        } 
        return spannable; 
    }


  

点击可以进行联网打电话等操作

Link文件

/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.apis.text;

import com.example.android.apis.R;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Html;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.StyleSpan;
import android.text.style.URLSpan;
import android.widget.TextView;

public class Link extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.link);

        // text1 shows the android:autoLink property, which
        // automatically linkifies things like URLs and phone numbers
        // found in the text.  No java code is needed to make this
        // work.

        // text2 has links specified by putting <a> tags in the string
        // resource.  By default these links will appear but not
        // respond to user input.  To make them active, you need to
        // call setMovementMethod() on the TextView object.

        TextView t2 = (TextView) findViewById(R.id.text2);
        t2.setMovementMethod(LinkMovementMethod.getInstance());

        // text3 shows creating text with links from HTML in the Java
        // code, rather than from a string resource.  Note that for a
        // fixed string, using a (localizable) resource as shown above
        // is usually a better way to go; this example is intended to
        // illustrate how you might display text that came from a
        // dynamic source (eg, the network).

        TextView t3 = (TextView) findViewById(R.id.text3);
        t3.setText(
            Html.fromHtml(
                "<b>text3:</b>  Text with a " +
                "<a href=\"http://www.google.com\">link</a> " +
                "created in the Java source code using HTML."));
        t3.setMovementMethod(LinkMovementMethod.getInstance());

        // text4 illustrates constructing a styled string containing a
        // link without using HTML at all.  Again, for a fixed string
        // you should probably be using a string resource, not a
        // hardcoded value.

        SpannableString ss = new SpannableString(
            "text4: Click here to dial the phone.");

        ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 6,
                   Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new URLSpan("tel:4155551212"), 13, 17,
                   Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        TextView t4 = (TextView) findViewById(R.id.text4);
        t4.setText(ss);
        t4.setMovementMethod(LinkMovementMethod.getInstance());
    }
}


备注:t2.setMovementMethod(LinkMovementMethod.getInstance());使textiew可以被点击
Link.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2007 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="wrap_content">

  <!-- Four TextView widgets, each one displaying text containing links. -->

  <!-- text1 automatically linkifies things like URLs and phone numbers. -->
  <TextView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/text1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:autoLink="all"
            android:text="@string/link_text_auto"
            />

  <!-- text2 uses a string resource containing explicit <a> tags to
       specify links. -->
  <TextView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/text2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="@string/link_text_manual"
            />

  <!-- text3 builds the text in the Java code using HTML. -->
  <TextView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/text3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />

  <!-- text4 builds the text in the Java code without using HTML. -->
  <TextView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/text4"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />

</LinearLayout>

备注自动识别链接

     <!-- android:autoLink="web"-设置自动识别链接,值web为匹配Web网址 -->

     <!--android:autoLink="phone"-设置自动识别链接,值phone为匹配电话号码 -->

     <!-- android:autoLink="email"-设置自动识别链接,值email为匹配Email地址 -->

     <!-- android:autoLink="all"-设置自动识别链接,值all为匹配所有 -->

string资源

在link_text_auto中表示连接的地方必需有空格或者换行

 <string name="link_text_auto"><b>text1:</b> This is some text.  In
      this text are some things that are actionable.  For instance,
      you can click on http://www.google.com and it will launch the
      web browser.  You can click on google.com too.  And, if you
      click on (415) 555-1212 it should dial the phone.
    </string>
    <string name="link_text_manual"><b>text2:</b> This is some other
      text, with a <a href="http://www.google.com">link</a> specified
      via an <a> tag.  Use a \"tel:\" URL
      to <a href="tel:4155551212">dial a phone number</a>.
    </string>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值