Android Studio 学习记录-文本视图(TextView)

        

目录

文本显示

设置文本内容

设置文本的大小 

设置文本颜色       

显示不下使用...省略

中划线、下划线

跑马灯


        本文介绍如何在文本视图TextView上显示规定的文本。

文本显示

设置文本内容

        在XML文件中通过属性android:text设置文本

        文本未经设置,默认出现在父控件左上角

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="你好,Android"/>

        Android Studio 不推荐在XML布局文件里直接写字符串,因为可能有好几个页面都显示“你好,Android”,如果要把这句话改为“你吃了吗?”就需要一个一个改。故Android Studio推荐把字符串放到位于res/values目录下strings.xml名为@string的地方。

        在strings.xml添加新的字符串定义 ,字符串名为“tv_HelloAndroid”,字符串值为“你好,Android”。

<string name="tv_HelloAndroid">你好,Android</string>

        添加完新的字符串定义,回到XML布局文件,将android:text属性值改为"@srting/字符串名",也就是“@string/tv_HelloAndroid”。

<TextView
        android:id="@+id/tv_HelloAndroid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tv_HelloAndroid"/>

设置文本的大小 

         在XML文件中通过属性android:textSize设置文本大小,并要求在字号数字后面写明单位类型。

        常见的字号单位主要有px、dp、sp三种 ,分别介绍如下。

        1.px

                px是手机屏幕的最小显示单位,它与设备的显示屏有关。一般来说,同样尺寸的屏幕,看起来越清晰,表示像素密度越高,以px计量的分辨率也越大。

        2.dp

                dp指的是与设备无关的显示单位,它只与屏幕尺寸有关。一般来说,同样尺寸的屏幕以dp计量的分辨率是相同的,比如同样6英寸手机,无论那个厂家生产,其分辨率换算成dp单位都是一个大小。

        3.sp

                sp的原理跟dp差不多,但它专门用来设置字体大小,也是Android推荐的字号单位。

<TextView
        android:id="@+id/tv_20sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tv_20sp"
        android:textSize="20sp"/>
    <TextView
        android:id="@+id/tv_50sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tv_50sp"
        android:textSize="50sp"/>

设置文本颜色       

         在XML文件中通过属性android:textColor设置文本颜色,但要给色值添加井号前缀(#)

<TextView
        android:id="@+id/tv_color"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tv_color"
        android:textColor="#00ff00"
        android:textSize="30sp"/>

        就像字符串资源那样,Android把颜色也当作一种资源,就在res/values目录下的colors.xml中

         在colors.xml添加新的颜色定义 ,颜色名为“green”,色值为“#00ff00”。

<color name="green">#00ff00</color>

        回到XML布局文件,把android:textColor的属性值改为"@color/颜色名称",也就是android:textColor="@color/green"

<TextView
        android:id="@+id/tv_color"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tv_color"
        android:textColor="@color/green"
        android:textSize="30sp"/>

显示不下使用...省略

        当所指定区域无法显示完整字符串时, 系统会尽可能在指定区域显示字符串,隐去无法显示的字符。这时我们可以使用android:ellipsize="end",让在显示不下时,在末尾用“...”显示。

<TextView
        android:id="@+id/tv_name..."
        android:layout_width="110dp"
        android:maxLines="1"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:text="@string/tv_name"
        android:textSize="25sp"/>

中划线、下划线

        中划线、下划线无法在布局文件中实现,需要在java文件里进行定义

        首先需要声明控件mTv1、mTv2, 然后通过findViewById()找到控件

        通过mTv1.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG)设置中划线

        通过mTv2.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG)设置下划线

        通过mTv1.getPaint().setAntiAlias(true)去除锯齿

private TextView mTv1;
    private TextView mTv2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_view);
        mTv1=findViewById(R.id.tv_name);
        mTv1.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        mTv1.getPaint().setAntiAlias(true);
        mTv2=findViewById(R.id.tv_name2);
        mTv2.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        mTv2.getPaint().setAntiAlias(true);
    }

跑马灯

        要做到跑马灯首先需要的属性是android:singleLIne="true"单行显示

        通过android:ellipsize="marquee"设置进行跑马灯循环

        通过android:marqueeRepeatLimit设置循环次数,marquee_forever为无限循环

        通过android:focusable="true"和android:focusableInTouchMode="true"设为焦点

<TextView
        android:id="@+id/tv_name3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tv_name3"
        android:textSize="24sp"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true"/>

         最后转到java文件mTv3,声明新控件并设置为mTv3.setSelected(true)

private TextView mTv1;
    private TextView mTv2;
    private TextView mTv3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_view);
        mTv1=findViewById(R.id.tv_name);
        mTv1.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        mTv1.getPaint().setAntiAlias(true);
        mTv2=findViewById(R.id.tv_name2);
        mTv2.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        mTv2.getPaint().setAntiAlias(true);
        mTv3=findViewById(R.id.tv_name3);
        mTv3.setSelected(true);
    }

猜你喜欢

转载自blog.csdn.net/demon_dog/article/details/128882709
今日推荐