安卓开发中TextView的常见用法(总结)

主要有以下几种样式:

1、带有阴影的TextView。

2、带有边框的TextView。(捎带做一个带有边框的EditText)

3、带有图标的TextView。

4、带有autoLink的TextView。(比如,链接百度等)

5、解析HTML标签的TextView。

6、解析SpannableString的TextView。(部分字体颜色、大小等变化)

7、带有跑马灯效果的TextView。

效果图如下:

安卓开发中TextView的常见用法(总结)_第1张图片


activity_main.xml的代码如下:




    
        

            

            

            

            

            

            

            
            

            
            

        

    

style_textview_border.xml 边框样式如下(改变组件外观固定样式的话,用shape):



    
    
    
    
    
    
    
    
    
    
    

MainActivity.java的代码如下:

package com.deepreality.textviewdemo;

import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ImageSpan;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.lang.reflect.Field;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EditText editText = findViewById(R.id.main_etWithBorder);
        editText.setFocusable(false);

        /*//手动设置带有图标的图标大小
        TextView tvWithIcon = (TextView) findViewById(R.id.main_tvWithIcon);
        Drawable[] drawable = tvWithIcon.getCompoundDrawables();
        // 数组下表0~3,依次是:左上右下
        drawable[1].setBounds(100, 0, 200, 200);
        tvWithIcon.setCompoundDrawables(null, drawable[1], null,
                null);*/


        //以下代码实现带有HTML标签的解析
        //:设置颜色和字体。
        //:设置字体大号
        //:设置字体小号
        //:斜体粗体
        //:连接网址
        //:图片
        TextView tvWithHTML = findViewById(R.id.main_tvWithHTML);
        String htmlWithImg = "百度一下,你就知道~:
图片:
"; //String html = "百度一下,你就知道~:
百度"; //调用Html.fromHtml()方法将字符串转换为CharSequence接口 //tvWithHTML.setText(Html.fromHtml(html)); //tvWithHTML.setMovementMethod(LinkMovementMethod.getInstance()); tvWithHTML.setText(Html.fromHtml(htmlWithImg, imageGetter, null)); TextView tvWithSpannableString = findViewById(R.id.main_tvWithSpannableString); StringBuilder sb = new StringBuilder(); for (int i = 0; i < 20; i++) { sb.append("好友" + (i + 1) + ","); } String likeUsers = sb.substring(0, sb.lastIndexOf(",")).toString(); tvWithSpannableString.setMovementMethod(LinkMovementMethod.getInstance()); tvWithSpannableString.setText(addClickPart(likeUsers), TextView.BufferType.SPANNABLE); } Html.ImageGetter imageGetter = new Html.ImageGetter() { @Override public Drawable getDrawable(String source) { Drawable draw = null; Field field = null; try { field = R.drawable.class.getField(source); int resourceId = Integer.parseInt(field.get(null).toString()); draw = getResources().getDrawable(resourceId); draw.setBounds(0, 0, draw.getIntrinsicWidth(), draw.getIntrinsicHeight()); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return draw; } }; //定义一个点击每个部分文字的处理方法 private SpannableStringBuilder addClickPart(String str) { //赞的图标,这里没有素材,就找个笑脸代替下~ ImageSpan imgspan = new ImageSpan(MainActivity.this, R.mipmap.collect); SpannableString spanStr = new SpannableString("p."); spanStr.setSpan(imgspan, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); //创建一个SpannableStringBuilder对象,连接多个字符串 SpannableStringBuilder ssb = new SpannableStringBuilder(spanStr); ssb.append(str); String[] likeUsers = str.split(","); if (likeUsers.length > 0) { for (int i = 0; i < likeUsers.length; i++) { final String name = likeUsers[i]; final int start = str.indexOf(name) + spanStr.length(); ssb.setSpan(new ClickableSpan() { @Override public void onClick(View widget) { Toast.makeText(MainActivity.this, name, Toast.LENGTH_SHORT).show(); } @Override public void updateDrawState(TextPaint ds) { super.updateDrawState(ds); //设置字体颜色,删除下划线 ds.setColor(getResources().getColor(R.color.colorPrimaryDark)); //ds.setUnderlineText(false); } }, start, start + name.length(), 0); } } return ssb.append("等" + likeUsers.length + "个人觉得很赞"); } }

你可能感兴趣的:(安卓开发中TextView的常见用法(总结))