使用Kotlin的Android TextView –全面教程

Kotlin is the official programming language for Android apps development. In this tutorial, we’ll be discussing TextViews in Android applications using Kotlin programming. We’ll create and change TextViews code in Kotlin programming.

Kotlin是用于Android应用程序开发的官方编程语言。 在本教程中,我们将讨论使用Kotlin编程的Android应用程序中的TextView。 我们将在Kotlin编程中创建和更改TextViews代码。

Android TextView概述 (Android TextView Overview)

Android TextView is a subclass of the View class. The View class typically occupy our window. TextView is used to display text on the screen. We can do a lot of fancy things using TextView.

Android TextView是View类的子类。 View类通常占据我们的窗口。 TextView用于在屏幕上显示文本。 我们可以使用TextView做很多花哨的事情。

Let’s start with a fresh project in Android Studio.

让我们从Android Studio中的一个新项目开始。

Create a new project and make sure you’ve enabled Kotlin in the setup wizard.

创建一个新项目,并确保已在设置向导中启用Kotlin。

在XML布局中创建TextView (Creating TextView in XML Layout)

TextView is created in an xml layout in the following manner.

TextView以以下方式在xml布局中创建。

<TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

The four attributes defined above are the core attributes of the TextView widget.

上面定义的四个属性是TextView小部件的核心属性。

  1. The id property is used to set a unique identifier. It’s set as @+id/ followed by the name you assign. The same name would be used to retrieve the TextView property in our Kotlin Activity class.

    id属性用于设置唯一标识符。 设置为@+id/后跟您分配的名称。 在我们的Kotlin Activity类中,将使用相同的名称来检索TextView属性。
  2. The text property is used to set the string text to be displayed in the TextView.

    text属性用于设置要在TextView中显示的字符串文本。
  3. As it is evident from the names itself, layout_width and layout_height are used to set the boundaries of the TextView. The wrap_content means wrapping the width, height to the length of the text. The match_parent means the TextView matches the width/height of the enclosed parent view. We can also set hardcoded values in dp (device independent pixels).

    从名称本身可以明显看出,layout_width和layout_height用于设置TextView的边界。 wrap_content表示将宽度,高度包装为文本的长度。 match_parent表示TextView与封闭的父视图的宽度/高度匹配。 我们还可以设置dp(与设备无关的像素)中的硬编码值。

Clean Code Tips: Instead of hardcoding the string, define it inside the strings.xml and set the text in the layout as follows.

干净的代码提示 :不用对字符串进行硬编码,而是在strings.xml内定义它,并按如下所示在布局中设置文本。

android:text="@string/app_name"

Let’s apply some attributes over the TextView in XML.

让我们在XML的TextView上应用一些属性。

TextView XML属性 (TextView XML Attributes)

Let’s give you a quick tour of some of the popular attributes of TextView widget.

让我们快速浏览一下TextView小部件的一些流行属性。

  • android:textSize: sets the size of the TextView. It’s recommended to use sp instead of dp. The sp stands for scale independent pixels and scales the font. Example: 16sp.

    android:textSize :设置TextView的大小。 建议使用sp代替dp。 sp代表与比例无关的像素并缩放字体。 示例:16sp。
  • The android:textColor is used to set a color for the text. Typically it is of the format #rgb, #rrggbb, #aarrggbb.

    android:textColor用于设置文本的颜色。 通常,其格式为#rgb,#rrggbb,#aarrggbb。
  • The android:background attribute is used to set the background color of the TextView.

    android:background属性用于设置TextView的背景颜色。
  • The android:textStyle is used to set the style among bold, italic, and normal. If you want to set bold and italic, use android:textStyle = “bold|italic”.

    android:textStyle用于在粗体,斜体和普通之间设置样式。 如果要设置粗体和斜体,请使用android:textStyle =“ bold | italic”。
  • The android:textAppearance attribute is used to set the style on a TextView, which includes its own color, font, and size. We can create custom styles in the styles.xml file.

    android:textAppearance属性用于在TextView上设置样式,该样式包括其自己的颜色,字体和大小。 我们可以在styles.xml文件中创建自定义样式。
  • android:visibility is used to set the visibililty of the text, possible values are visible, invisible, and gone. The gone makes the textview invisible and removes it from the current position in the layout.

    android:visibility用于设置文本的可见性,可能的值是visibleinvisiblegonegone使textview不可见,并将其从布局中的当前位置删除。
  • The android:ellipsize is used to handle situations when the length of the text exceeds the limit.

    android:ellipsize用于处理文本长度超过限制的情况。
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值