安卓TextView的常用属性

 一、TextView的常用基础属性:

  1. android:id: 为TextView设置一个组件id,根据这个id我们可以在Java代码中通过findViewById()方法获取到该对象,然后进行相关属性的设置及操作。 
  2. android:layout_width:组件的宽度,一般写:"wrap_content"或者"match_parent(fill_parent)"。"wrap_content":包含内容,意思是内容有多少该组件的宽度就有多少;"match_parent":匹配父控件,意思是上一个控件的宽度有多少该控件的宽度就有多少;也可以自定义大小,比如设置成200dp。
  3. android:layout_height:组件的高度,和上面的layout_width的设置一样,也是"wrap_content"或者是"match_parent"。
  4. android:text:设置显示的文本内容,一般我们是把字符串写到string.xml文件中,然后通过@String/xxx取得对应的字符串内容,当然也可以直接写到android:text:""里面,但是一般不建议这么写。写到string.xml文件中的好处就是方便我们更改文字,比如我们的项目有很多相同的文字但是后来我们的项目发布到外国去了,需要翻译成英文的,如果我们直接在这个text中写那么需要我们把整个.xml布局文件中的文字一个一个的更改过来。比如我们的软件各个界面里有很多“关注”,如果直接写在android:text:""里面,我们需要把每个“关注”改成"attention",如果我们将这个“关注”写在string.xml文件中,我们只需要将string.xml中的   <string name="attention">关注</string>  改为 <string name="attention">attention</string> 就可以将各个界面的“关注”全部改成“attention”了。
  5. android:textColor:设置字体颜色,同上,通过colors.xml资源来引用。
  6. android:textStyle:设置字体风格,三个可选值:"normal"(无效果),"bold"(加粗),"italic"(斜体)。可以使用"|"设置多个样式,比如android:textStyle="italic|bold"将文字变为斜体并且加粗。
  7. android:textSize:字体大小,单位一般是用sp!
  8. android:background:控件的背景颜色,可以理解为填充整个控件的颜色,也可以是图片。
  9. android:gravity:设置控件中内容的对齐方向。注意如果要用gravity属性,此组件的layout_width和layout_height不能设置为wrap_content。此时设置的gravity属性没有效果,因为组件包裹着内容,无论设置什么,也都不能有改变。

 activity_main.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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv_message"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:text="@string/hello"
        android:textColor="@color/red"
        android:textStyle="italic|bold"
        android:textSize="30sp"
        android:background="@color/yellow"
        android:gravity="center"/>

</LinearLayout>

string.xml文件:

<resources>
    <string name="app_name">TextViewTest</string>
    <string name="hello">大家好!我是ACfun!</string>
</resources>

colors.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#6200EE</color>
    <color name="colorPrimaryDark">#3700B3</color>
    <color name="colorAccent">#03DAC5</color>
    <color name="red">#FF0000</color>
    <color name="yellow">#FFFF00</color>
</resources>

运行效果:


 二、TextView的其他属性

  1. android:maxLines文本的最大行数,如果超过这个最大行数文本将不会显示
  2. android:minLines文本的最小行数,如果超过了这个行数文本将自动换行到下一行继续显示
  3. android:ellipsize:当字符内容过长无法全部显示时可以用省略号来代替未显示的字符。有5个可选项"start":省略号在开头;"middle":省略号在中间;"end":省略号在结尾;"marquee":跑马灯效果;"none":没有效果。注意:1、需要设置循环的次数,这里使用android:marqueeRepeatLimit="marquee_forever"设置为永久循环。2、实现跑马灯,必须让该控件获得焦点,使用:android:focusable="true"     android:focusableInTouchMode="true"两条语句实现让该控件获得焦点。如果你用的AndroidStudio自带的模拟器,如果你的的api小于26需要加上android:clickable="true"语句,点一下那一行文字才可以实现跑马灯的效果。如果是用的手机则不加第三句也可以,会自动跑起来。

  4. android:singleLine: 将文字是否单行显示,如果是“true”则设置为单行显示,“false”则不为单行显示。
  5. android:layout_marginTop:当前组件距离其父组件在上方向上的边距,单位为dp。  同样还有左右边距等。

在上面的activity_main.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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv_message"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:background="@color/yellow"
        android:gravity="center"
        android:text="@string/hello"
        android:textColor="@color/red"
        android:textSize="30sp"
        android:textStyle="italic|bold" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/blue"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:gravity="top"
            android:text="@string/message"
            android:textSize="30sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/blue"
            android:clickable="true"
            android:ellipsize="marquee"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:gravity="center"
            android:marqueeRepeatLimit="marquee_forever"
            android:singleLine="true"
            android:text="@string/message"
            android:textColor="@color/red"
            android:textSize="30sp"
            android:textStyle="italic" />

    </LinearLayout>


</LinearLayout>

string.xml文件:

<resources>
    <string name="app_name">TextViewTest</string>
    <string name="hello">大家好!我是ACfun!</string>
    <string name="message">大家好!我是ACfun!感谢阅读我的博客,如果有不同的见解或者补充,欢迎评论哟!</string>
</resources>

color.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#6200EE</color>
    <color name="colorPrimaryDark">#3700B3</color>
    <color name="colorAccent">#03DAC5</color>
    <color name="red">#FF0000</color>
    <color name="yellow">#FFFF00</color>
    <color name="blue">#00CCFF</color>
</resources>

运行效果:


发布了81 篇原创文章 · 获赞 108 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_41575507/article/details/105504387