android控件——textView使用

拼搏现实的明天。 2022-09-25 09:28 311阅读 0赞

textView控件

一、功能简介:

  1. TextView可以说是 Android中最简单的一个控件了,你在前面其实也已经和它打过了一 些打交道。它主要用于在界面上显示一段文本信息。

二、创建textView文件:

  1. 以下是在layout.xml布局文件中的创建testView空间的一段代码:
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >
  3. <TextView android:id="@+id/text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textSize="24sp" android:textColor="#00ff00" android:text="This is TextView" />
  4. </LinearLayout>

属性介绍:

1.1 android:id=”@+id/text_view” 给当前控件定义了 一个唯一标识符

1.2 android:layout_widthh=”match_parent” 指定了 控件的宽度

1.3 android:layout_heighth=”wrap_content” 指定了控件的高度

1.4 android:gravity=”center” 来指定文字的对齐方式。

1.5 android:textSize=”24sp” 指定文字的大小

1.6 android:textColor=”#00ff00” 指定文字的颜色。

……..

  1. 1. layout_widthlayout_height属性值说明:
  2. Android中所有的控件都具有layout_widthlayout_height这两个属性,可选值有三种 match_parentfill_parent wrap_content,其中 match_parent fill_parent的意义相同,现在官方更加推荐使用 match_parent
  3. match_parent表示让当前控件
  4. 的大小和父布局的大小一样,也就是由父布局来决定当前控件的大小。wrap_content表示让 当前控件的大小能够刚好包含住里面的内容,也就是由控件内容决定当前控件的大小。所以 上面的代码就表示让 TextView的宽度和父布局一样宽,也就是手机屏幕的宽度,让 TextView 的高度足够包含住里面的内容就行
  5. 2. gravity属性值说明:
  6. 可选值有 topbottomleftrightcenter | "center" "center_vertical|center_horizontal",表示文字在垂直和水平方向都居中对齐。

效果:

效果

发表评论

表情:
评论列表 (有 0 条评论,311人围观)

还没有评论,来说两句吧...

相关阅读