腊月的季节

Android文本框TextView

补充知识

  • dp(dip): device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA、HVGA和QVGA 推荐使用这个,不依赖像素。
  • px: pixels(像素). 不同设备显示效果相同,一般我们HVGA代表320x480像素,这个用的比较多。
  • pt: point,是一个标准的长度单位,1pt=1/72英寸,用于印刷业,非常简单易用
  • sp: scaled pixels(放大像素). 主要用于字体显示best for textsize。

基本属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<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"
tools:context=".MainActivity"
android:gravity="center"
android:background="#8fffad">

<TextView
android:id="@+id/txtOne"
android:layout_width="200dp"
android:layout_height="200dp"
android:gravity="center"
android:text="TextView(显示框)"
android:textColor="#EA5246"
android:textStyle="bold|italic"
android:background="#000000"
android:textSize="18sp" />

</RelativeLayout>
  • id:为TextView设置一个组件id,我们可以在java代码中通过findViewById()的方法获取到该对象,然后进行相关属性的设置,又或者使用RelativeLayout时,参考组件用的也是id。
  • layout_width:组件的宽度,一般写:wrap_content或者match_parent,前者是控件显示的内容多大,控件就多大,后者会填满该控件所在的父容器,当然也可以设置成特定的大小。
  • layout_height:组件的宽度,同上
  • gravity:设置空间中内容的对齐方向。
  • text:设置文本的内容,一般我们是把字符串写到string.xml然后通过@String/xxx取得对应的字符串内容的。
  • textColor:设置字体颜色,通过colors.xml
  • textStyle:设置字体风格,可选值:normal,bold,italic(斜体)
  • textSize:字体大小,一般用sp单位
  • background:空间的背景颜色,可以是图片。

开发例子

  • android:shadowColor:设置阴影颜色,需要与shadowRadius一起使用哦!
  • android:shadowRadius:设置阴影的模糊程度,设为0.1就变成字体颜色了,建议使用3.0
  • android:shadowDx:设置阴影在水平方向的偏移,就是水平方向阴影开始的横坐标位置
  • android:shadowDy:设置阴影在竖直方向的偏移,就是竖直方向阴影开始的纵坐标位置
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <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" />

drawable的TextView

drawableTop(上),drawableButtom(下),drawableLeft(左),drawableRight(右)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<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"
tools:context="com.jay.example.test.MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:drawableTop="@drawable/show1"
android:drawableLeft="@drawable/show1"
android:drawableRight="@drawable/show1"
android:drawableBottom="@drawable/show1"
android:drawablePadding="10dp"
android:text="张全蛋" />

</RelativeLayout>

热评文章