一、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"><TextViewandroid: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"><TextViewandroid: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" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/blue"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:gravity="top"android:text="@string/message"android:textSize="30sp" /><TextViewandroid: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>

运行效果:


安卓TextView的常用属性相关推荐

  1. Android修行手册-TextView不常用属性篇

    我于杀戮之中盛放,亦如黎明中的花朵. 往期文章分享 点击跳转=>熬夜再战Android从青铜到王者-开发效率插件篇 点击跳转=>Unity粒子特效系列-龙卷风预制体做好了,unitypac ...

  2. TextView之二:常用属性

    参考自<疯狂android讲义>2.3节 //TextView所呈现的文字 android:text="我爱Java" //文字颜色 android:textColor ...

  3. android中用代码设置edittext属性为密码,Android中EditText常用属性设置

    EditText继承关系:View–>TextView–>EditText 常用属性如下:android:layout_gravity="center_vertical" ...

  4. iOS开发之TextView常用属性

    基本属性: //textView尺寸和位置let textViewWidth: CGFloat = 223let textViewHeight: CGFloat = 198let textViewTo ...

  5. android button 属性,两行显示数字,前面各有图像,Android_2_常用控件及常用属性

    安卓常用控件及其常用属性 TextView android:id 这是唯一地标识控件的ID. android:capitalize 如果设置,指定该TextView中有一个文本输入法 会自动利用什么类 ...

  6. Android_2_常用控件及常用属性

    安卓常用控件及其常用属性 TextView android:id 这是唯一地标识控件的ID. android:capitalize 如果设置,指定该TextView中有一个文本输入法 会自动利用什么类 ...

  7. 学习笔记之——Android常用属性归纳

    本篇博客仅作为个人学习笔记所用,各种Android常用属性归纳长期更新,如有错误和建议还望指点~ 1.控件透明度和半透明度: 半透明<Button Android:background=&quo ...

  8. Android相对布局(RelativeLayout)常用属性、练习使用按键、文本框等控件、线性布局(LinearLayout)属性

    RelativeLayout中子控件常用属性: 子控件默认是从父控件的左上角开始排列的 相对于父控件 android:layout_alignParentTop="true" 和父 ...

  9. ScrollView和HorizontalScrollView常用属性,及禁止滑动

    常用属性:详见注释 activity_main_28 <?xml version="1.0" encoding="utf-8"?> <Line ...

  10. 安卓笔记10.常用布局——相对布局

    目录 零.学习目标 一.相对布局概述 1.布局特点 2.继承关系图 3.常用属性 (1)相对于父容器居中 (2)相对于其它控件的位置 (3)相对于其它控件对齐 (4)相对于父容器对齐 (5)标识符问题 ...

最新文章

  1. thinkpad e40 热键hotkey失效解决办法
  2. python画柱状图代码-python plotly画柱状图代码实例
  3. PHP连接达梦数据库
  4. python读取序列5之后的数据_Python核心编程读笔 5: python的序列
  5. pthread_create会导致内存泄露
  6. 100内奇数之和流程图_JavaScript基础教程(六)流程控制之循环语句
  7. js学习总结--持续更新(2)
  8. node ajax crud,基于node.js和rethinkdb的CRUD(增删改查)Web服务
  9. DBA最缺的不是技术
  10. PHPExcel 插件使用详解
  11. MangoDB索引、排序和聚合
  12. Kaggle——TMDB 5000 Movie Dataset电影数据分析
  13. SpringBoot 2.X 整合 J2cache 一级缓存 ehcache3 二级缓存 redis (含使用demo实例)
  14. 小米手机开启开发者模式的步骤与方法
  15. Python operator.ge()函数与示例
  16. 手机性能指标详细测试步骤【Android/IOS】
  17. CATIA CAA二次开发专题(四)------创建自己的Addin
  18. VB:如何设置Richtextbox的行间距
  19. x264代码剖析(二):如何编译运行x264以及x264代码基本框架
  20. protobuf 微信小程序_常州微信小程序开发-Unity3D使用Protobuf、ProtobufHelper

热门文章

  1. python导入鸢尾花数据集_python KNN算法实现鸢尾花数据集分类
  2. Python之OpenGL笔记(36):环境光下的棋盘球体
  3. 计算机房通气换气次数,各种换气次数汇总
  4. xps 转 pdf android版,xps文件转换pdf
  5. Red5流媒体服务器初探——Red5服务器的搭建
  6. ubuntun安装扫描仪
  7. android动画哪些,Android Animation动画(很详细)
  8. Kafka的常用命令(包括:下载安装、后台启动)
  9. python mock server_python学习笔记6--mockserver
  10. linux系统有gotoxy函数,gotoxy() implementation for Linux using printf