TextView使用实例

简介: 第一次发文章好紧张哦,一定要我。一、引用1、TextView实战之你真的懂我么?2、Android TextView 添加下划线的几种方式3、Android在一个TextView里显示不同样式的字体4、盘点Android使用自定义字体遇到...

第一次发文章好紧张哦,一定要我。

一、引用

1、TextView实战之你真的懂我么?
2、Android TextView 添加下划线的几种方式
3、Android在一个TextView里显示不同样式的字体
4、盘点Android使用自定义字体遇到的坑
5、Android应用使用第三方字体

二、实例

1、完全靠xml布局实现的各式文字样式(超链接,下划线,滚动,阴影,拉伸,粗体斜体,图片加载)

图例:
img_d89608c751d73ffee22a8d81cc1162f7.png
tv-1.png

xml:

<!--part 1-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:orientation="vertical">

            <TextView
                style="@style/text_part_title_style"
                android:layout_margin="3dp"
                android:text="@string/textview_title_1" />
            <!--none|all|web|phone|mail|map-->
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="3dp"
                android:autoLink="web"
                android:text="www.baidu.com" />

            <!--代码跑起来才知道-->
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="3dp"
                android:text="@string/textview_line" />

            <TextView
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_margin="3dp"
                android:maxLength="8"
                android:text="测试长度1234566777" />

            <!--start,end,middle分别是在哪里省略,marquee是滚动,android4.x error-->
            <TextView
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_margin="3dp"
                android:ellipsize="marquee"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:marqueeRepeatLimit="marquee_forever"
                android:maxEms="8"
                android:singleLine="true"
                android:text="数据太长只能滚动看123456" />

            <TextView
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_margin="3dp"
                android:singleLine="true"
                android:text="测试单行test1234567890" />

            <TextView
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_margin="3dp"
                android:maxLines="2"
                android:text="测试最多2行test1234567890xxxxxx" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="3dp"
                android:digits="123456"
                android:text="测试只能数字test1234567890xxxxxx" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="3dp"
                android:hint="测试默认隐藏文字"
                android:textColorHint="@color/green" />

            <!--斜体在4.x上不行,要写nomospace?? , 阴影得运行才看得见-->
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="3dp"
                android:background="@color/wx_blue"
                android:drawableLeft="@mipmap/android_label"
                android:gravity="center"
                android:minHeight="30dp"
                android:padding="10dp"
                android:scaleY="1.2"
                android:shadowColor="@color/red"
                android:shadowDx="15.0"
                android:shadowDy="5.0"
                android:shadowRadius="2.5"
                android:text="布局实现:大小,颜色,最低高度,居中,背景色,内距,拉伸_文字间隔,阴影,粗体斜体,加载图片"
                android:textColor="@color/white"
                android:textSize="18sp"
                android:textStyle="italic|bold"
                android:typeface="monospace" />


        </LinearLayout>

2、textview和string.xml的部分使用(格式化文字和string-array)

图例:
img_f9dd929cc39d6750c98db0703fecd7cd.png
tv-2.png

xml:

<!--part 2-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:background="@color/white"
            android:orientation="vertical">

            <TextView
                style="@style/text_part_title_style"
                android:layout_margin="3dp"
                android:text="@string/textview_title_2" />

            <TextView
                android:id="@+id/tv_string"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="3dp"
                android:text="@string/textview_string" />

            <TextView
                android:id="@+id/tv_string_format"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="3dp"
                android:text="@string/textview_string_format" />

        </LinearLayout>

string.xml等:

<!--main-->
    <string-array name="android_study_modules">
        <item>组件</item>
        <item>控件</item>
        <item>数据库</item>
        <item>通信</item>
        <item>多媒体</item>
        <item>文件</item>
        <item>设备</item>
    </string-array>

<string name="textview_string_format">测试StringFormat:StringArray有%d个数据=%s,可测试Float=%2f</string>

代码:

/**
     * 测试string.xml的使用
     * XLIFF,全称叫 XML 本地化数据交换格式,英文全称 XML Localization Interchange File Format
     * */
    private void testStringXml(){
        //normal
        tv_string.setText(R.string.textview_string);

        //super
        String[] array_string = getResources().getStringArray(R.array.android_study_modules);
        LogUtil.i("array_string.length="+array_string.length+",content="+ Arrays.toString(array_string));

        String format_string = getString(R.string.textview_string_format);
        LogUtil.i("format_string org = "+format_string);
        format_string = String.format(format_string,array_string.length,Arrays.toString(array_string),3.333f);
        LogUtil.i("format_string java code = "+format_string);
        tv_string_format.setText(format_string);

        format_string = getString(R.string.textview_string_format,array_string.length,Arrays.toString(array_string),3.333f);
        LogUtil.i("format_string android code = "+format_string);
        tv_string_format.setText(format_string);
    }

3、代码实现各种文字样式(SpannableString的运用,文字内部超链接,下划线,滚动,阴影,拉伸,粗体斜体,图片加载,第三方字体,奇奇怪怪的文字)

图例:
img_2866ced0674fef028cf03cb5805fc979.png
tv-3.png

xml:

<!--part 3-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:background="@color/white"
            android:orientation="vertical">

            <TextView
                style="@style/text_part_title_style"
                android:layout_margin="3dp"
                android:text="@string/textview_title_3" />

            <TextView
                android:id="@+id/tv_code_italic"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="3dp"
                android:text="代码实现:斜体" />

            <TextView
                android:id="@+id/tv_code_bottom_line"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="3dp"
                android:text="代码实现:下划线" />

            <TextView
                android:id="@+id/tv_code_autolink"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="3dp"
                android:text="代码实现:AutoLinkALL" />

            <TextView
                android:id="@+id/tv_code_autolink2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="3dp"
                android:text="代码实现:定制化AutoLink" />

            <TextView
                android:id="@+id/tv_code_asset"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="3dp"
                android:text="代码实现:第三方asset字体应用" />

            <TextView
                android:id="@+id/tv_code_fun_style"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="3dp"
                android:text="代码实现:乱七八糟,大小上下颜色各不同的文字,真的很好玩" />

            <TextView
                android:id="@+id/tv_code_diff_style"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:text="代码实现:大小,颜色,最低高度,居中,背景色,内距,拉伸_文字间隔,阴影,粗体斜体,加载图片" />


        </LinearLayout>

代码:

/**
     * 测试斜体
     * 貌似必须加上MONOSPACE,否则中文字无法斜体
     * */
    private void testItalic(){
        String aaa = tv_code_italic.getText().toString();

        /*方法1:全部是斜体粗体*/
        tv_code_italic.setTypeface(Typeface.MONOSPACE,Typeface.BOLD_ITALIC);

        /*方法2:部分斜体(中文字的话没法斜体)*/
        //SpannableString spn = new SpannableString(aaa);
        //what,start,end,flag
        //spn.setSpan(spn,0,3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        //tv_code_italic.setText(spn);

        /*方法3:使用画笔(中文字的话没法斜体)*/
        //tv_code_italic.getPaint().setFakeBoldText(true);

    }

    /**
     * 测试中划线/下划线
     * */
    private void testTxtLine(){
        /*方法1:paint : STRIKE_THRU_TEXT_FLAG=中划线(删除线) , UNDERLINE_TEXT_FLAG=下划线*/
        tv_code_bottom_line.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);//getPaint().setFlags();
        tv_code_bottom_line.getPaint().setAntiAlias(true);//抗锯齿

        /*方法2:html标签类文字*/
        //String htmlString = "<u>我是html下划线</u>";
        //tv_code_bottom_line.setText(Html.fromHtml(htmlString));

        /*方法3:SpannablString类,一般用于某个文字下划线*/
        //String spanString = "我是spna文字,啦啦1234";
        //SpannableString content = new SpannableString(spanString);
        //content.setSpan(new UnderlineSpan(), 0, spanString.length(), 0);
        //tv_code_bottom_line.setText(content);
    }

    /**
     * 测试autoLink内容
     * none|web|phone|mail|map
     * */
    private void testAutoLink(){
        /*固定的 autolink all*/
        final String contact = "Email: xxxxxx@qq.com\n" +
                "Phone: 189111111111\n" +
                "Fax: +47-12345678\n" +
                "HTTP: http://www.baidu.com";
        tv_code_autolink.setAutoLinkMask(Linkify.ALL); // or set 'android:autoLink' in layout xml
        tv_code_autolink.setText(contact);

        /*定制的 autolink all*/
        //将TextView的显示文字设置为SpannableString
        tv_code_autolink2.setText(getClickableSpan());
        //设置该句使文本的超连接起作用
        tv_code_autolink2.setMovementMethod(LinkMovementMethod.getInstance());
    }


    //设置超链接文字
    private SpannableString getClickableSpan() {
        SpannableString spanStr = new SpannableString("使用该软件,即表示您同意该软件的使用条款和隐私政策");
        //设置下划线文字
        spanStr.setSpan(new UnderlineSpan(), 16, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //设置文字的单击事件
        spanStr.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                //startActivity(new Intent(MainActivity.this, UsageActivity.class));
                ToastUtil.showShort(instance,"点击了使用条款");
            }
        }, 16, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //设置文字的前景色
        spanStr.setSpan(new ForegroundColorSpan(Color.GREEN), 16, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        //设置下划线文字
        spanStr.setSpan(new UnderlineSpan(), 21, 25, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //设置文字的单击事件
        spanStr.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                //startActivity(new Intent(MainActivity.this, PrivacyActivity.class));
                ToastUtil.showShort(instance,"点击了隐私政策");
            }
        }, 21, 25, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //设置文字的前景色
        spanStr.setSpan(new ForegroundColorSpan(Color.GREEN), 21, 25, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        return spanStr;
    }

    /**
     * 测试第三方asset中字体
     * 如果全局引用第三方字体两种比较好方式
     * 1、MyApplication通过反射定义此typeface,然后在theme中引用,然后套用此theme
     *    优化一点,可以通过注解+反射的方式,类似bufferfly框架中
     * 2、各种自定义控件中属性使用新typeface
     * 3、单个使用慢慢设置...
     *
     * 全局使用请看HelloApp.java这个Application文件
     * */
    private void testAssetFont(){
        //maybe exception
        Typeface ttf = Typeface.createFromAsset(getAssets(),"fonts/SIMKAI.TTF");
        tv_code_asset.setTypeface(ttf);
    }

    /**
     * 测试同一段文字显示乱七八糟有趣的
     * 主要是SpannableString
     * */
    private void testFunnyTextView(){
        String funnyString = tv_code_fun_style.getText().toString();
        SpannableString spna = new SpannableString(funnyString);
        // 字体颜色
        spna.setSpan(new ForegroundColorSpan(Color.RED), 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        // 背景色
        spna.setSpan(new BackgroundColorSpan(Color.RED), 2, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        // 粗体
        spna.setSpan(new StyleSpan(Typeface.BOLD), 4, 6, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        // 字体大小
        spna.setSpan(new AbsoluteSizeSpan(50), 6, 8, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        // 下划线
        spna.setSpan(new URLSpan(""), 8, 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        // 删除线(中划线)
        spna.setSpan(new StrikethroughSpan(), 10, 12, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        // 胖瘦字体
        spna.setSpan(new ScaleXSpan(2.0f), 12, 13, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        spna.setSpan(new ScaleXSpan(0.5f), 13, 14, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        // 显示
        tv_code_fun_style.setText(spna);
    }

    /**
     * 测试用代码输出风格迥异的文字
     * 设置各种尺寸要用到sp,dp和px,最多是px
     * */
    private void testTextViewCode(){

        //设置背景色,其他:setBackgroundResource(resid),setBackground(drawable)
        tv_code_diff_style.setBackgroundColor(AppUtil.getColor(instance,R.color.wx_blue));
        //设置字体颜色
        tv_code_diff_style.setTextColor(Color.WHITE);
        //设置大小
        tv_code_diff_style.setTextSize(18f);//默认用的sp为单位,不需要修改
        //设置宽度:非固定高度的,设置match_parent等需要从他父控件的param开始设置
        //tv_code_diff_style.setWidth(100);//pix
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        //params.width = 100;params.height=100;params.weight=1;
        tv_code_diff_style.setLayoutParams(params);
        //设置居中
        tv_code_diff_style.setGravity(Gravity.CENTER);
        //设置最低高度
        tv_code_diff_style.setMinHeight(DisplayUtil.dip2px(instance,30f));//pix
        //设置内距
        tv_code_diff_style.setPadding(10,10,10,10);
        //设置拉伸_文字间隔
        tv_code_diff_style.setScaleY(1.2f);//setScaleX
        //设置阴影
        tv_code_diff_style.setShadowLayer(2.5f,15f,5f,Color.RED);
        //设置斜体粗体
        tv_code_diff_style.setTypeface(Typeface.MONOSPACE,Typeface.BOLD_ITALIC);
        //设置加载图片
        Drawable drawable= getResources().getDrawable(R.mipmap.android_label);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());//必有,不然不显示
        tv_code_diff_style.setCompoundDrawables(drawable,null,null,null);
    }
目录
相关文章
|
Android开发 数据安全/隐私保护
Android TextView 使用以及属性(方法)大全(下)
TextViewXML属性和相关方法说明(2)
928 0
|
6天前
|
XML Java 开发工具
TextView
TextView
28 0
|
9月前
|
Java Android开发 iOS开发
Android TextView 与 EditText 的区别与使用
Android TextView 与 EditText 的区别与使用
105 0
|
Android开发
|
XML Java 开发工具
TextView(下)
TextView(下)
76 0
|
XML Android开发 数据格式
Android ImageView及其子类 介绍+实例(下)
ImageButton 什么是ImageButton 通过实例了解ImageButton 1、创建布局文件 运行效果如下: ImageButton灰色边框的产生原因和解决方案 QuickContactBadge 什么是QuickContactBadge QuickContactBadge的调用方法 通过实例了解QuickContactBadge 1、创建布局文件 2、让QuickContactBadge与特定联系人建立联系 3、运行效果
192 0
Android ImageView及其子类 介绍+实例(下)
|
XML 缓存 前端开发
Android ImageView及其子类 介绍+实例(上)
ImageView ImageView的继承关系 ImageView支持的常用XML属性 ImageView.ScaleType所支持的常量 ImageView加载图片 实例:用ImageView做一个图片查看器 1、创建布局文件 2、用代码使ImageView显示图片 3、运行效果 ImageView设置图片 设置圆角、圆形 1、通过RoundedBitmapDrawable设置圆角、圆形 2、使用图片加载框架进行设置,以Picasso为例
384 0
Android ImageView及其子类 介绍+实例(上)
|
存储 XML 自然语言处理
Android TextView 使用以及属性(方法)大全(中)
TextViewXML属性和相关方法说明(1)
907 0
|
XML Android开发 数据格式
Android TextView 使用以及属性(方法)大全(上)
TextView是什么 使用TextView 1.在xml中创建并设置属性 2.在xml中创建,在代码中设置属性 效果图: 布局文件 : 在代码中实现: 运行结果分析 3.在代码中创建并设置属性
350 0
Android TextView 使用以及属性(方法)大全(上)