先介绍几个单位:
dp
(dip
):device independent pixels
,译为设备独立像素。px
:pixels
,译为像素。pt
:point
是一个标准的长度单位,1pt
是1/72
英寸。sp
:scaled pixels
,译为放大像素。
基础属性详解
通过下面这个简单的界面,来了解几个最基本的属性:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#8fffad"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:id="@+id/txtOne"
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#000000"
android:gravity="center"
android:text="TextView(显示框)"
android:textColor="#EA5246"
android:textSize="18sp"
android:textStyle="bold|italic" />
</RelativeLayout>
上面的TextView
中有下述几个属性:
id
:为TextView
设置一个组件id
。根据id
,我们可以在Java
代码中通过findViewById
的方法获取到该对象,然后进行相关属性的设置;或者使用RelativeLayout
时,参考组件用的也是id
。layout_width
:组件的宽度,一般写wrap_content
或者match_parent
(fill_parent
),前者是控件显示的内容多大,控件就多大,而后者会填满该控件所在的父容器。当然也可以设置成特定的大小,比如这里为了显示效果,设置成了200dp
。layout_height
:组件的宽度,内容同上。gravity
:设置控件中内容的对齐方向,TextView
是对齐文字,ImageView
是对齐图片。text
:设置显示的文本内容。一般我们是把字符串写到string.xml
文件中,然后通过@String/xxx
取得对应的字符串内容,这里为了方便直接就写到该属性里,不建议这样写。textColor
:设置字体颜色,一般通过colors.xml
资源来引用。textStyle
:设置字体风格,三个可选值:normal
(无效果)、bold
(加粗)、italic
(斜体)。textSize
:字体大小,单位一般是用sp
。background
:控件的背景颜色,可以理解为填充整个控件的颜色,也可以是图片。
实际开发例子
带阴影的TextView
涉及到的几个属性:
android:shadowColor
:设置阴影颜色,需要与shadowRadius
一起使用。android:shadowRadius
:设置阴影的模糊程度,设为0.1
就变成字体颜色了,建议使用3.0
。android:shadowDx
:设置阴影在水平方向的偏移,就是水平方向阴影开始的横坐标位置。android:shadowDy
:设置阴影在竖直方向的偏移,就是竖直方向阴影开始的纵坐标位置。
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:shadowColor="#F9F900"
android:shadowDx="10.0"
android:shadowDy="10.0"
android:shadowRadius="3.0"
android:text="带阴影的TextView"
android:textColor="#4A4AFF"
android:textSize="30sp" />
带边框的TextView
如果你想为TextView
设置一个边框背景(普通矩形边框或者圆角边框),下面可能帮到你。另外TextView
是很多其他控件的父类(比如Button
),也可以设置这样的边框。实现原理很简单,编写一个ShapeDrawable
的资源文件,然后TextView
将background
设置为这个drawable
资源即可。
简单说一下shapeDrawable
资源文件的几个节点以及属性:
<solid android:color = "xxx">
:这个是设置背景颜色的。<stroke android:width = "xdp" android:color="xxx">
:这个是设置边框的粗细以及边框颜色的。<padding androidLbottom = "xdp">
:这个是设置边距的。<corners android:topLeftRadius="10px">
:这个是设置圆角的。<gradient>
:这个是设置渐变色的,可选属性如下:
startColor
:起始颜色。endColor
:结束颜色。centerColor
:中间颜色。angle
:方向角度,等于0
是从左到右,然后逆时针方向转;等于90
度时,从下往上。type
:设置渐变的类型。
在Drawable
目录下新建文件txt_rectborder.xml
:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 设置一个黑色边框 -->
<stroke
android:width="2px"
android:color="#000000" />
<!-- 渐变 -->
<gradient
android:angle="270"
android:endColor="#C0C0C0"
android:startColor="#FCD209" />
<!-- 设置一下边距,让空间大一点 -->
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
</shape>
在Drawable
目录下新建文件txt_radiuborder.xml
:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 设置透明背景色 -->
<solid android:color="#87CEEB" />
<!-- 设置一个黑色边框 -->
<stroke
android:width="2px"
android:color="#000000" />
<!-- 设置四个圆角的半径 -->
<corners
android:bottomLeftRadius="10px"
android:bottomRightRadius="10px"
android:topLeftRadius="10px"
android:topRightRadius="10px" />
<!-- 设置一下边距,让空间大一点 -->
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
</shape>
将TextView
的background
属性设置成上面这两个Drawable
:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/txtOne"
android:layout_width="200dp"
android:layout_height="64dp"
android:background="@drawable/txt_rectborder"
android:gravity="center"
android:text="矩形边框的TextView"
android:textSize="18sp" />
<TextView
android:id="@+id/txtTwo"
android:layout_width="200dp"
android:layout_height="64dp"
android:layout_marginTop="10dp"
android:background="@drawable/txt_radiuborder"
android:gravity="center"
android:text="圆角边框的TextView"
android:textSize="18sp" />
</LinearLayout>