关于android:Android入门教程-TextView简介宽高文字间距

2次阅读

共计 3220 个字符,预计需要花费 9 分钟才能阅读完成。

TextView 简介

文字,是咱们传播信息的一种常见形式。在安卓利用上显示文字,咱们通常应用 TextView。之前咱们曾经晓得如何获取到 layout 中的 TextView,也晓得 setText() 办法能够批改显示的文字。

联合咱们理论的生存和学习教训,写字的时候,有哪些方面是能够由咱们来管制的?文本内容;文字色彩;大小;背景等等。

最简略的 TextView

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

得益于 as 弱小的提醒性能,咱们在 layout 中输出 <Te 的时候,as 可能就弹出了提醒。

回车或者鼠标双击 TextView 即可。

这里关注两个根本属性 layout_widthlayout_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); // 应用字体

间距设置

marginpadding 其实是 View 的属性。这里咱们拿 TextView 来看一下。

当前想显示一些文字的时候,咱们首先会想起的是 TextView。

Android 零根底入门教程视频参考

正文完
 0