2019日更挑战(三),android-聊聊TextView

瞎扯

TextView.个人认为是android开发中用的最频繁的一个控件了.
非常强大,绝不是只简单的显示文字而已.

常见的几种写法,效果:

1.图片加文字的条目

image.png

是不是一个控件就搞定了一个条目.

 <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ff0"
        android:drawableLeft="@mipmap/ic_launcher"
        android:drawableRight="@drawable/ic_arrow_forward_black_24dp"
        android:drawablePadding="8dp"
        android:gravity="left"
        android:padding="8dp"
        android:text="1111111" />

2. 带选择器的,选中效果的

image.png
 <TextView
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_marginTop="10dp"
        android:background="#0ff"
        android:drawableLeft="@drawable/ic_check_circle_black_24dp"
        android:drawableRight="@drawable/ic_arrow_forward_black_24dp"
        android:drawablePadding="8dp"
        android:gravity="center_vertical"
        android:enabled="true"
        android:padding="8dp"
        android:text="22222" />

3.上下结构icon,带省略号:

image.png
    <TextView
            android:layout_width="90dp"
            android:layout_height="wrap_content"
            android:background="#edf"
            android:drawableTop="@drawable/ic_home_black_24dp"
            android:drawablePadding="8dp"
            android:ellipsize="end"
            android:gravity="center"
            android:lines="1"
            android:padding="8dp"
            android:text="3333333333" />

4.能够点击,显示超链接的

image.png
   <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:autoLink="all"
            android:background="#dfe"
            android:drawableTop="@mipmap/ic_launcher"
            android:drawablePadding="8dp"
            android:gravity="center"
            android:lines="1"
            android:padding="8dp"
            android:text="http://www.baidu.com " />

5.展示多行的

image.png
 <RelativeLayout
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/tv_4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#dfe"
            android:drawableTop="@mipmap/ic_launcher"
            android:drawablePadding="8dp"
            android:gravity="left"
            android:padding="8dp"
            android:text="444\n44444444" />

        <TextView
            android:layout_marginLeft="8dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/tv_4"
            android:background="#dfe"
            android:drawableLeft="@mipmap/ic_launcher"
            android:drawablePadding="8dp"
            android:padding="8dp"
            android:text="555\n55555\n55555555555\n555555555555555" />
    </RelativeLayout>

是不是很强大.但TextView并不是没有缺点

实际写起来你就会发现.不能控制icon的大小.

不过这没什么,git上有可以控制drawable大小的自定义TextView.


支持html格式解析.

这也是TextView的一大亮点

Html.ImageGetter imgGetter = new Html.ImageGetter() {
        public Drawable getDrawable(String source) {
            //这里加载图片资源.
            return drawable;
        }
    };
Spanned text = Html.fromHtml(htmlStr, imgGetter, null);
textView.setText(text);
然后就可以愉快的图文混排了.

当然,webview也是可以做到的.而且不用操作图片加载的问题.这个就看个人选择了.


强大的Spanned

这个东西非常厉害,为啥厉害呢?
看个效果图,就拿简书的来说


image.png

如果是新手,写这个.肯定是

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

我相信肯定,肯定有人会这样写,然后调大小,调边距....然后如果很多地方都有这种
就每个xmlcopy,copy.


实际上呢.
这个效果只需要一个TextView就足够了.
然后再写个工厂,生成一下.不管哪里用到都一行代码搞定.

如下:


image.png
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        parseText((TextView) findViewById(R.id.tv_6), "哈哈", "0.173", 11, 22, 33);
    }

    public void parseText(TextView textView, String name, String str0, int str1, int str2, int str3) {
        float textSize = textView.getTextSize();
        String text = "#1" + str0 + "\t" + name + "\t#2" + str1 + "\t#3" + str2 + "\t#4" + str3;
        SpannableStringBuilder spannableBuilder = new SpannableStringBuilder(text);
        Resources resources = getResources();
        //添加钻石图标
        int i = text.indexOf("#1");//这个#1,相当于一个占位符
        int iLength = "#1".length();//拿到占位符的长度.
        ImageSpan star = getImageSpan((int) textSize, resources.getDrawable(R.drawable.ic_star_black_24dp));
        //这里就是把对应的#1占位符替换成icon图标
        //需要4个参数,ImageSpan,替换的起点,终点,替换模式.
        spannableBuilder.setSpan(star, i, i + iLength, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

        //设置红色
        spannableBuilder.setSpan(new ForegroundColorSpan(resources.getColor(R.color.colorAccent)), i + iLength,
                str0.length() + i + iLength, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        //添加眼睛图标及数量
        int i2 = text.indexOf("#2");
        ImageSpan eye = getImageSpan((int) textSize, resources.getDrawable(R.drawable.ic_remove_red_eye_black_24dp));
        spannableBuilder.setSpan(eye, i2, i2 + 2, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

        //添加评论图标及数量
        int i3 = text.indexOf("#3");
        ImageSpan sms = getImageSpan((int) textSize, resources.getDrawable(R.drawable.ic_sms_black_24dp));
        spannableBuilder.setSpan(sms, i3, i3 + 2, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

        //添加心图标及数量
        int i4 = text.indexOf("#4");
        ImageSpan favorite = getImageSpan((int) textSize, resources.getDrawable(R.drawable.ic_favorite_black_24dp));
        spannableBuilder.setSpan(favorite, i4, i4 + 2, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

        textView.setText(spannableBuilder);
    }

    @NonNull
    private ImageSpan getImageSpan(int textSize, Drawable drawable) {
        drawable.setBounds(0, 0, textSize, textSize);
        return new CenterAlignImageSpan(drawable);
    }
}

居中显示的ImageSpan

/**
* 居中显示的ImageSpan
**/
    public class CenterAlignImageSpan extends ImageSpan {
        public CenterAlignImageSpan(Drawable drawable) {
            super(drawable);
        }

        public CenterAlignImageSpan(Bitmap b) {
            super(b);
        }

        public void draw(@NonNull Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, @NonNull Paint paint) {
            Drawable b = this.getDrawable();
            Paint.FontMetricsInt fm = paint.getFontMetricsInt();
            int transY = (y + fm.descent + y + fm.ascent) / 2 - b.getBounds().bottom / 2;
            canvas.save();
            canvas.translate(x, (float) transY);
            b.draw(canvas);
            canvas.restore();
        }
    }

看起来很麻烦.对不对.封装一下.就很简单了


交流群:493180098,这是个很少吹水,交流学习的群.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容