TextView
TextView文本框组件用于显示文字,继承自android.view.View,在android.widget包中.
常用属性 | 解释 |
---|---|
android:autoLink | 当文本为url连接时可设置是否显示为可点击连接(none/web/email/phone/map/all) |
android:linksClickable | 设置链接是否点击连接,即使设置了autoLink |
android:drawableBottom | 在text的下方输出一个drawable资源可以是一张图片。如果指定一个颜色的话会把text的背景设为该颜色,并且同时和background使用时覆盖后者 |
android:drawableLeft | 在text的左边输出一个drawable资源 |
android:drawablePadding | 设置text与drawable资源的间隔,与drawableLeft、drawableRight、drawableTop、drawableBottom一起使用,可设置为负数,单独使用没有效果 |
android:drawableRight | 在text的右边输出一个drawable资源 |
android:drawableTop | 在text的正上方输出一个drawable资源 |
android:editable | 设置是否可编辑 |
android:lines | 设置文本的行数,设置两行就显示两行,即使第二行没有数据 |
android:maxLength | 限制显示的文本长度,超出部分不显示 |
android:password | 以小点”.”显示文本,在密码输入时使用 |
android:scrollHorizontally | 设置文本超出TextView的宽度的情况下,是否出现横拉条 |
简单例子:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/tv_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="正常显示"
android:textColor="#000000"
android:textSize="24sp"/>
<TextView
android:id="@+id/tv_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="斜体"
android:textColor="#000000"
android:textSize="24sp"
android:textStyle="italic"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/tv_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跑马灯---跑马灯---跑马灯"
android:textColor="#000000"
android:textSize="24sp"
android:layout_marginTop="10dp"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"/>
</LinearLayout>
EditText
常用属性 | 解释 |
---|---|
android:digits | 限制允许输入的字符 |
android:hint | 设置提示文字 |
android:singleLine | 是否单行显示 |
android:maxLength | 限制最大输入字符 |
android:inputType | 设置输入框的类型,例如text、number、phone、textUri、textPassword |
以一个简单登录页面为例:
<?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:orientation="vertical">
<EditText
android:id="@+id/login_accout"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="100dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:drawablePadding="8dp"
android:hint="请输入手机号码/邮箱地址" />
<EditText
android:id="@+id/login_password"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:drawablePadding="8dp"
android:hint="请输入密码" />
<Button
android:id="@+id/login_login"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="70dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="登录" />
</LinearLayout>