zoukankan      html  css  js  c++  java
  • Android开发 TextView的开发记录

    前言

      此篇博客是记录一些TextView开发上一些少用的开发功能项.目前开发记录如下:

    •   添加图片
    •   文字滚动
    •   添加省略号
    •   实现长文的收起和展开功能
    •   改变一个字符串里自定字符的颜色或者大小

    效果字体(粗体/斜体/下划线)

    增加效果字体的方法有很多既可以在xml属性里设置,又可以在代码里设置.(这里我们除了不演示使用SpannableString实现方式,都会演示一下)

    xml里设置粗体

    android:textStyle="bold"

     <TextView
            android:id="@+id/html5_test"
            android:gravity="start"
            android:text="测试文本"
            android:textSize="17sp"
            android:textStyle="bold"
            android:layout_width="200dp"
            android:layout_height="50dp" />

    xml里设置斜体

    android:textStyle="italic"

    <TextView
            android:id="@+id/html5_test"
            android:gravity="start"
            android:text="测试文本"
            android:textSize="17sp"
            android:textStyle="italic"
            android:layout_width="200dp"
            android:layout_height="50dp" />

    代码里实现粗体/斜体/下划线/中划线

    这是实现方式其实就是获取TextView绘制文字的paint,通过改变paint的配置.达到改变文字效果

    粗体

       TextView textView = findViewById(R.id.text_view);
       TextPaint paint = textView.getPaint();
       paint.setFakeBoldText(true);

    下划线/中划线

    textView1.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);//中划线
    textView1.getPaint().setAntiAlias(true); //去掉锯齿
    textView2.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线

    添加图片

     <!--TextView 放入图片例子-->
            <!--android:drawableTop="@drawable/icon1"  在文字上面放入图片-->
            <!--android:drawablePadding="20dp"  设置图片与文字之间的间隔-->
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="努力学习"
                android:textColor="#0000ff"
                android:textSize="50sp"
                android:drawableTop="@drawable/icon1"
                android:drawablePadding="20dp"/>

    代码里添加图片

                Drawable drawable = context.getDrawable(R.drawable.ic_selection);
                drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
                itemName.setCompoundDrawables(null, null, drawable, null);

    这里说明一下getDrawable的Drawable是没有大小尺寸的需要自己重新设定。如果不明白参考:https://www.cnblogs.com/guanxinjing/p/11249427.html

    文字滚动播放

      <!--设置有滚动播放效果的文字显示-->
            <!--android:singleLine="true"  设置单行-->
            <!--android:marqueeRepeatLimit="marquee_forever"  设置滚动次数,这里为永久滚动-->
            <!--android:ellipsize="marquee"  ellipsize意思省略位置,marquee的意思是滚动模式-->
            <!--android:focusable="true"  意思可聚焦,被选中。只有聚焦的文字才会滚动-->
            <!--android:focusableInTouchMode="true"   可调焦的触摸模式-->
            <!--注意此方法设置文字滚动,一个页面只有一段文字可以被预设聚焦并且滚动-->
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/main4Text1"
                android:layout_marginTop="30dp"
                android:singleLine="true"
                android:ellipsize="marquee"
                android:marqueeRepeatLimit="marquee_forever"
                android:focusable="true"
                android:focusableInTouchMode="true"/>

    添加省略号

    android:maxLines="2"
    android:ellipsize="end"

    获取光标框选文本的位置

    int start = mTextView.getSelectionStart();
    int end = mTextView.getSelectionEnd();

    实现长文的收起和展开功能

    关键一  在TextView里面的getLayout方法

    /**
         * Gets the {@link android.text.Layout} that is currently being used to display the text.
         * This value can be null if the text or width has recently changed.
         * @return The Layout that is currently being used to display the text.
         */
        public final Layout getLayout() {
            return mLayout;
        }

    关键二  在Layout里的getEllipsisCount方法

     /**
         * Returns the number of characters to be ellipsized away, or 0 if
         * no ellipsis is to take place.
         */
        public abstract int getEllipsisCount(int line);

    使用方式

    //获取省略的字符数,0表示没省略
    int ellipsisCountholder = textView.getLayout().getEllipsisCount(holder.content.getLineCount()-1);

    注意,TextView还在初始化的时候getLayout()可能会返回null,所以需要在TextView初始化测绘完成后获取.如下:

    holder.content.post(new Runnable() {
                    @Override
                    public void run() {
                        //获取省略的字符数,0表示没和省略
                        int ellipsisCountholder = holder.content.getLayout().getEllipsisCount(holder.content.getLineCount()-1);
                        if (ellipsisCountholder != 0){
                            holder.telescopicContent.setText("全文");
                            holder.telescopicContent.setVisibility(View.VISIBLE);
                        }
    
                    }
                });

    去上面的代码配合,实现TextView的收起与展开

    viewHolder.telescopicContent.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (viewHolder.telescopicContent.getText().equals("全文")){
                            viewHolder.telescopicContent.setText("收起");
                            viewHolder.content.setMaxLines(50);
    
                        }else {
                            viewHolder.telescopicContent.setText("全文");
                            viewHolder.content.setMaxLines(4);
    
                        }
    
                    }
                });

    改变一个字符串里自定字符的颜色或者大小

       SpannableString spannableString = new SpannableString("今天天气不错");
        spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#FF0000")), 2, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv5.setText(spannableString);

    setSpan方法有四个参数,

    第一个参数:ForegroundColorSpan是为文本设置前景色,也就是文字颜色。如果要为文字添加背景颜色,可替换为BackgroundColorSpan。

    第二个参数:文本颜色改变的起始位置

    第三个参数:文本颜色改变的结束位置。

    最后一个参数为布尔型,可以传入以下四种:

    • Spanned.SPAN_INCLUSIVE_EXCLUSIVE 从起始下标到终止下标,包括起始下标
    • Spanned.SPAN_INCLUSIVE_INCLUSIVE 从起始下标到终止下标,同时包括起始下标和终止下标
    • Spanned.SPAN_EXCLUSIVE_EXCLUSIVE 从起始下标到终止下标,但都不包括起始下标和终止下标
    • Spanned.SPAN_EXCLUSIVE_INCLUSIVE 从起始下标到终止下标,包括终止下标

    文字间距

    设置文字之间的间距

    android:letterSpacing="0.5"

    设置行间距

    android:lineSpacingExtra="10dp"

    设置行间距倍数

    android:lineSpacingMultiplier="0.5"

    字体阴影效果

    水平阴影偏移量

    android:shadowDx="1" 

    垂直阴影偏移量

    android:shadowDy="1" 

    阴影颜色

    android:shadowColor="#8c8c8c" 

    阴影范围

    android:shadowRadius="5"

    文字颜色点击/选中后改变

    在res创建color目录,在目录下面创建如下文件:

    selected_text_color.xml

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_focused="true" android:color="@color/color_text_yellow"/>
        <item android:state_checked="true" android:color="@color/color_text_yellow"/>
        <item android:state_pressed="true" android:color="@color/color_text_yellow"/>
        <item android:state_selected="true" android:color="@color/color_text_yellow"/>
        <item android:color="@color/color_55"/>
    </selector>

    在TextView里使用它

        <TextView
            android:id="@+id/name"
            android:text="name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/selected_text_color"/>

    设置TextView可以上下滑动内容

     在设置一些文字内容超出,但是又需要固定高度的时候。我们可以让TextView实现滑动功能起来。

    在xml里添加一下2个属性:

    android:scrollbars="vertical"
    android:fadeScrollbars="false"

    在代码里设置:

    mTextView.setMovementMethod(ScrollingMovementMethod.getInstance());

    如果想自定义滚动条,接着在xml里面加入属性:

    android:scrollbarThumbVertical="@drawable/ic_launcher"   //滑块的图片
    android:scrollbarTrackVertical="@drawable/ic_launcher"   //滑道的图片 

    根据TextView尺寸改变文字的大小

    注意,以下属性是Android8.0支持的,低于8.0使用app:兼容属性比如: app:autoSizeTextType="uniform"。

    另外,下面这些属性在Button上也支持的。

    开关自动改变字体大小

    有2个属性 none 与 uniform, uniform为开启,开启后文字会尽可能的填充满TextView所有空白的空间。

            android:autoSizeTextType="uniform"

    设置自动改变字体大小的最大值与最小值

    当TextView尺寸改变后,文字也会自动改变大小。但是只能在这两个限制属性之间

            android:autoSizeMaxTextSize="60sp"
            android:autoSizeMinTextSize="10sp"

    设置每次自动改变字体大小的步值

    当TextView尺寸改变后,文字也会自动改变大小。但是,每次改变增大或者减小会不规则,你可以使用下面这个属性限定每次增大与减小的值。如下属性每次变化只会以5sp

            android:autoSizeStepGranularity="5sp"

    预设尺寸范围

    设置了 Autosizeing 的粒度,就可以在这个范围内,根据我们设置的粒度进行缩放。通常,使用粒度来控制基本上可以达到我们的要求,但是如果对缩放有更精准的要求,例如:[10.15,40,60,100] 这样的缩放,使用粒度就达不到我们的要求了。

    预设尺寸可以接受一个尺寸数组,Autosizeing 就会从我们设定的尺寸数组中,取一个尺寸进行设置。同时你可以为这些尺寸设置一个统一的尺寸单位。

    如果想要在 layout-xml 使用属性的形式使用预设尺寸,你首先需要一个 array 的资源,然后通过 autoSizePresetSizes 属性进行设置即可。

    array 资源的格式:

    <resources>
      <array name="autosize_text_sizes">
        <item>10sp</item>
        <item>12sp</item>
        <item>20sp</item>
        <item>40sp</item>
        <item>100sp</item>
      </array>
    </resources>

    定义好 array 的尺寸资源之后,就可以在 layout-xml 中使用它。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
      <TextView
          android:layout_width="match_parent"
          android:layout_height="200dp"
          app:autoSizeTextType="uniform"
          app:autoSizePresetSizes="@array/autosize_text_sizes" />
    </LinearLayout>
     

    END

    android:autoSizeTextType="uniform"
  • 相关阅读:
    wso2使用
    wso2安装
    CLR 编译函数的两种结果的原因
    hduoj4311
    通过Git在本地局域网中的两台电脑间同步代码
    Git基本操作之强制推送覆盖仓库
    设置Mac共享网络给其他设备
    谷歌浏览器设置无图浏览模式
    加载到SGA中的库缓存对象超过阈值
    webBrowser 禁止屏蔽alert confirm open showModalDialog
  • 原文地址:https://www.cnblogs.com/guanxinjing/p/11061514.html
Copyright © 2011-2022 走看看