TextView是android最基本的view之一,其有许多常用的用法:
<?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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp" //设置整个TextView与其他组件的间距
android:background="#0000ff" //设置整个TextView的背景颜色
android:padding="20dp" //设置TextView中文字与边框的距离
android:text="15395380870"
android:textColor="#ff0000" //设置文字的颜色
android:textSize="20dp"
android:typeface="serif" //设置字体
android:autoLink="phone" /> //设置自动链接到phone、email等应用
<TextView
android:id="@+id/mtv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#0000ff"
android:padding="20dp"
android:text="@string/myweb" //设置链接到个人网页,在Java代码总也要设置
android:textColor="#ff0000"
android:textSize="20dp"
android:typeface="serif"
/>
<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#0000ff"
android:ellipsize="end" //设置文字超出限制时的处理策略
android:padding="20dp"
android:singleLine="true" //设置文字单行显示
android:text="thisdfsdfsdfdsfdsfsdhfjhsjdisdfadfdftestsfkdhksdfkds!"
android:textColor="#ff0000"
android:shadowColor="#00ff00" //设置文字阴影的颜色
android:shadowDx="1" //设置文字阴影的x方向上的偏移
android:shadowDy="0" //设置文字阴影的Y方向上的偏移
android:shadowRadius="2" //设置文字阴影半径
android:textSize="20dp"
android:typeface="serif" />
<TextView
android:id="@+id/tv"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#0000ff"
android:ellipsize="marquee" //设置跑马灯效果,要配合Java代码
android:focusable="true" //设置可得到焦点
android:focusableInTouchMode="true" //设置点击可得到焦点
android:marqueeRepeatLimit="marquee_forever" //设置跑马灯效果策略
android:padding="20dp"
android:singleLine="true"
android:text="thisdfsdfsdfdsfdsfsdhfjhsjdisdfadfdftestsfkdhksdfkds!"
android:textColor="#ff0000"
android:textSize="20dp"
android:typeface="serif" />
<Button
android:id="@+id/btn"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="test"
android:focusableInTouchMode="true"/>
<EditText
android:id="@+id/et"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="test"
android:focusableInTouchMode="true"/>
</LinearLayout>
部分Java代码
TextView tv = (TextView) findViewById(R.id.tv);
tv.requestFocusFromTouch();//设置跑马灯
TextView mtv = (TextView) findViewById(R.id.mtv);
mtv.setMovementMethod(LinkMovementMethod.getInstance());//设置个人网页