•引言
在开始本节内容前,先要介绍下几个单位:
•属性介绍
id : 为TextView设置一个组件 id
layout_width : 组件的宽度
layout_height : 组件的高度,内容同上
textSize : 字体大小,单位一般是用 sp
textColor : 设置字体颜色
- maxLines : 设置文本框最多显示多少行字符
textStyle : 设置字体风格
ellipsize : 有多种取值
- android:ellipsize = "end" 省略号在结尾
- android:ellipsize = "start" 省略号在开头
- android:ellipsize = "middle" 省略号在中间
- android:ellipsize = "marquee" 跑马灯(最好加一个约束android:singleline = "true")
textScaleX:控制字体水平方向的缩放,默认值1.0f,值是float
linespacingExtra : 设置行间距
linespacingMultiplier : 设置行间距的倍数
•常规属性
<?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:background="@color/white" android:gravity="center" android:orientation="vertical" android:padding="15dp"> <TextView android:id="@+id/tv_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Android Studio Study" android:textColor="@color/black" android:textSize="20sp" /> </LinearLayout>
•运行效果
•textStyle
<?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:background="@color/white" android:gravity="center" android:orientation="vertical" android:padding="15dp"> <TextView android:id="@+id/tv_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Android Studio Study" android:textColor="@color/black" android:textSize="20sp" android:textStyle="bold|italic"/> </LinearLayout>
•运行效果
•带阴影的 TextView
涉及到的几个属性:
- android:shadowColor : 设置阴影颜色,需要与 shadowRadius 一起使用
- android:shadowRadius : 设置阴影的模糊程度,设为 0.1 就变成字体颜色了,建议使用 3.0
- android:shadowDx : 设置阴影在水平方向的偏移,就是水平方向阴影开始的横坐标位置
- android:shadowDy : 设置阴影在竖直方向的偏移,就是竖直方向阴影开始的纵坐标位置
<?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" android:orientation="vertical" android:padding="15dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="带阴影的 TextView" android:textColor="@color/black" android:textSize="30sp" android:shadowColor="@color/gray" android:shadowDx="10.0" android:shadowDy="10.0" android:shadowRadius="3.0"/> </LinearLayout>
•运行效果
•带图片的 TextView
设置图片的核心其实就是 : drawableXxx;
可以设置四个方向的图片 :
- drawabletop(上)
- drawableButtom(下)
- drawableLeft(左)
- drawableRight(右)
另外,你也可以使用 drawablePadding 来设置图片与文字间的间距。
<?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" android:orientation="vertical" android:padding="15dp"> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableLeft="@drawable/apple" android:drawabletop="@drawable/apple" android:drawableRight="@drawable/apple" android:drawableBottom="@drawable/apple" android:drawablePadding="10dp" android:text="苹果" android:textSize="20sp" /> </LinearLayout>
•运行效果
•调整图片大小
我们这样设置的 drawable 并不能在 XML 调整图片大小;
需要在通过 Java 代码来进行调整。
public class Test extends AppCompatActivity { private TextView mTv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test_menu); mTv = findViewById(R.id.tv); Drawable[] drawable = mTv.getCompoundDrawables(); //drawable[0,1,2,3] 依次代表 : 左上右下 drawable[1].setBounds(10, 0, 110, 100); mTv.setCompoundDrawables(drawable[0], drawable[1], drawable[2],drawable[3]); } }代码说明:
•运行效果
•使用autoLink属性识别链接类型
当文字中出现了 URL,E-Mail,电话号码,地图的时候;
我们可以通过设置autoLink属性;
当我们点击 文字中对应部分的文字,即可跳转至某默认 APP;
比如一串号码,点击后跳转至拨号界面!
<?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" android:gravity="center"> <TextView android:id="@+id/tv_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="12345678910" android:textSize="20sp" android:autoLink="phone" /> <TextView android:id="@+id/tv_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="12345678910" android:textSize="20sp" android:autoLink="all" /> </LinearLayout>
•运行效果
•实现跑马灯效果
<?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" android:gravity="center" android:padding="10dp"> <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18sp" android:singleLine="true" android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever" android:focusable="true" android:focusableInTouchMode="true" android:text="Test Marguee,Test Marguee,Test Marguee,Test Marguee,Test Marguee"/> </LinearLayout>需要注意的是,设置的 Text 内容长度需要超出 layout_width 的长度才会滚动。
•运行效果
•参考资料
[1] : 视频链接
[2] : 菜鸟教程
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。