目录
前言
前面学了Android的五大基本布局,在安卓API文档还发现了其他布局。
诸如:PercentRelativeLayout ,CoordinatorLayout , DrawerLayout...
我们可暂做了解,当有实际使用需求的时候再进行进一步的学习。
下面直接快马加鞭进入UI控件 - TextView 的学习。
基础应用
相关属性
id:为TextView设置一个组件id,可在Java代码中通过 findViewById() 方法获取到该对象,前面我们使用相对布局(RelativeLayout)时,参考组件即设置为id。
layout_width / layout_height:组件的宽度/高度,一般设置为 "wrap_content" (大小刚好足够显示当前控件里的内容)、"match_parent (fill_parent)"(填满该控件所在的父容器) ;当然也可以直接设置具体的值。
gravity:设置控件中内容的对齐方向。
text:设置显示的文本内容。
textColor:设置字体颜色。
textStyle:设置字体风格,可设置为 "normal"(无效果), "bold"(加粗), "italic"(斜体)
textSize:字体大小,单位一般是用 sp 。
background:控件的背景颜色,可以理解为填充整个控件的颜色,也可以设置图片填充。
示例
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:id="@+id/text1"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:background="#000000"
android:gravity="center"
android:text="Text - test"
android:textColor="#00f00f"
android:textSize="40sp"
android:textStyle="bold|italic" />
</LinearLayout>