posts - 103,  comments - 41,  views - 20万

TextView是简单而又复杂的控件,简单是使用上,复杂是源代码研究。基础编程这里只讲是如何使用。

TextView主要分为两种使用方法,一种是xml中不带span的textview,另外一种是Span的textview。

1.因为是在democoderjoy中使用,这里我们新建一个TextviewActi的activity。

public class TextViewActi extends Activity {
    @Override
    protected void onCreate(Bun。。。。

2.直接上布局不带span的布局TextView的使用。

复制代码
<TextView
        android:id="@+id/txt1One"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:text="Textview demo 大家来学习 啊啊 zcx zcx zcx zcx,呵呵呵呵呵呵呵呵~"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:shadowColor="#F9F900"
        android:shadowDx="10.0"
        android:shadowDy="10.0"
        android:shadowRadius="3.0"
        android:text="带阴影的TextView"
        android:textColor="#4A4AFF"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/txtOne"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:textSize="18sp"
        android:gravity="center"
        android:background="@drawable/txt_rectborder"
        android:text="矩形边框的TextView" />

    <TextView
        android:id="@+id/txtTwo"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:layout_marginTop="10dp"
        android:textSize="18sp"
        android:gravity="center"
        android:background="@drawable/txt_radiuborder"
        android:text="圆角边框的TextView" />

    <TextView
        android:id="@+id/zhang"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:drawableTop="@mipmap/ic_launcher"
        android:drawableLeft="@mipmap/ic_launcher"
        android:drawableRight="@mipmap/ic_launcher"
        android:drawableBottom="@mipmap/ic_launcher"
        android:drawablePadding="10dp"
        android:text="张全蛋" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="www.baidu.com"
        android:autoLink="web"/>
    <TextView
        android:id="@+id/txtThree"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_marginTop="10dp"
        android:textSize="18sp"
        android:gravity="center"
         />
    <TextView
        android:id="@+id/txtFor"
        android:layout_width="wrap_content"
        android:layout_height="80dp"

        android:textSize="16sp"

    />
View Code
复制代码

从上到下布局分别代表是跑马灯效果、带阴影的、矩形边框、圆角边框、四周图片、超链接、(span使用)、文字点击效果(span)。

3.带span的效果采用SpannableStringBuilder和SpannableString这个来使用

参考代码如下

复制代码
TextView t2 = (TextView) findViewById(R.id.txtThree);

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

4.还有一个html的TextVIew。参考代码中使用

TextView t1 = (TextView)findViewById(R.id.txtOne);
        String s1 = "<font color='blue'><b>百度一下,你就知道~:</b></font><br>";
        s1 += "<a href = 'http://www.baidu.com'>百度</a>";
        t1.setText(Html.fromHtml(s1));
        t1.setMovementMethod(LinkMovementMethod.getInstance());

 

实验效果如下图:

 

 

参考链接

posted on   zCoderJoy  阅读(212)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示