TextView简介
文字,是咱们传播信息的一种常见形式。在安卓利用上显示文字,咱们通常应用TextView。 之前咱们曾经晓得如何获取到layout中的TextView,也晓得setText()
办法能够批改显示的文字。
联合咱们理论的生存和学习教训,写字的时候,有哪些方面是能够由咱们来管制的? 文本内容;文字色彩;大小;背景等等。
最简略的TextView:
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" />
得益于as弱小的提醒性能,咱们在layout中输出<Te的时候,as可能就弹出了提醒。
回车或者鼠标双击TextView即可。
这里关注两个根本属性layout_width
和layout_height
。别离示意TextView的宽度和高度设置。 实际上这两个属性是View的属性。TextView继承自View。宽高属性是根底属性,是必须设置的。
宽和高属性
layout_width/layout_height 能够填入wrap_content,match_parent或者具体的数值。
- wrap_content:示意控件宽/高度可由内容来决定。对于TextView,文字越长,它的宽度越宽,直到父view(下层容器)容许的最大宽/高度。
- match_parent:示意控件宽/高度达到父view容许的最大值。艰深说就是把空间撑满。
- 咱们也能够输出具体数值。比方80dp。 dp是安卓中的一种单位,通常用来规定控件的宽高,距离间隔等等。相似的,示意文字大小的单位,安卓里用sp。
显示文字
显示文字,可能是 TextView 最次要的用法了。在 layout 中设置文字,应用 text 属性。
<TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="老手教程" /> <TextView android:id="@+id/sample_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name" />
这里波及到一个代码格调的问题。下面别离给TextView设置了id。有的人喜爱驼峰格调的,例如sampleTv。
咱们能够看到,设置text有多种形式。能够间接把内容写进去(hard code),也能够应用string资源。 间接写内容,as会给一个黄色的正告,倡议用户换用@string资源的形式。鼠标移上去as就能够看到as的正告了。
若要应用@string资源,咱们先看另一个xml文件,即strings.xml。它在res/values外面。
<string name="app_name">2021</string>
资源命名格调也是小写字母加下划线。
res外面的很多资源,咱们能够都能够用R…来找到。
后面咱们提到,能够应用 TextView 的 setText 办法来设置文字内容,例如setText(“123”)。 也能够传入文字资源的名称(编号),相似setText(R.string.app_name)。 须要留神的是,R.string.app_name 自身是一个 int 数字,TextView 会依据这个编号去找对应的资源。 如果这样调用 setText(123),大概率会报上面的这个谬误。
android.content.res.Resources$NotFoundException: String resource ID #0x0 at android.content.res.Resources.getText(Resources.java:360)
文字设置
一般来说,咱们会设置TextView文字的色彩,背景等等。
- textColor 设置字体色彩
- textSize 设置字体大小
- textStyle 设置字体款式
示例:
textStyle 设置字体款式
- normal 没有特殊效果,默认值
- italic 斜体
- bold 粗体
xml 中设置:
示例1:设置斜体
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Fisher" android:textColor="#000000" android:textStyle="italic" />
成果:
示例2:设置斜体并且加粗
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="bold|italic" android:textColor="#000000" android:textStyle="bold|italic" />
成果:
代码中设置
应用 TextView 的 setTypeface 办法来设置字体成果。
tv1.setTypeface(null, Typeface.NORMAL); // 一般 tv1.setTypeface(null, Typeface.BOLD); // 加粗 tv2.setTypeface(null, Typeface.ITALIC); // 斜体 tv3.setTypeface(null, Typeface.BOLD_ITALIC); // 加粗和斜体
setTypeface(@Nullable Typeface tf, @Typeface.Style int style)有2个参数。 第一个是字体,这里能够疏忽。 第二个是成果,有失常,加粗,斜体,加粗和斜体这几种可选。
字体(字库)
默认状况下,TextView 的 typeface 属性反对 sans、serif和monospace 这三种字体。 零碎默认 sans 作为文本显示的字体。但这三种字体只反对英文。如果显示中文,无论抉择这三种字体中的哪一种,显示成果都是一样的。
layout中设置字体:
应用 android:typeface
来设置字体。
<!-- sans字体 --> <TextView android:text="Hello,World" android:typeface="sans" /> <!-- serifs字体 --> <TextView android:text="Hello,World" android:typeface="serif" /> <!-- monospace字体 --> <TextView android:text="Hello,World" android:typeface="monospace" />
代码中应用字体:
tv.setTypeface(Typeface.SERIF); tv.setTypeface(Typeface.SANS_SERIF); tv.setTypeface(Typeface.MONOSPACE);
引入字体库
须要引入ttf字体文件。把字体文件放在assets/font目录里。 代码中应用AssetManager来获取字体。
例如:在Activity中设置字体。
TextView tv1 = findViewById(R.id.tv1); Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/otherFont.ttf"); tv1.setTypeface(tf); // 应用字体
间距设置
margin
和 padding
其实是 View 的属性。这里咱们拿 TextView 来看一下。
当前想显示一些文字的时候,咱们首先会想起的是TextView。
Android零根底入门教程视频参考