美文网首页
TextView知识点总结和demo实现

TextView知识点总结和demo实现

作者: AnQuestionBoy | 来源:发表于2017-11-15 14:13 被阅读0次

1,是什么?
TextView(文本框),用于显示文本的一个控件;
2,基本属性
1)id:为TextView设置一个组件id,根据id,我们可以在Java代码中通过 findViewById()的方法获取到该对象,然后进行相关属性的设置;
2)layout_width:组件的宽度,一般写:wrap_contentmatch_parent,前者是控件显示的内容多大,控件就多大,而后者会填满该控件所在的父容器,当然也可以设置成特定的大小,比如我这里为了显示效果,设置成了200dp;
3)layout_height:组件的宽度,跟layout_width类比使用;
gravity:设置控件中内容的对齐方向,TextView中是文字,ImageView中是图片等等。
4)text:设置显示的文本内容,一般我们是把字符串写到string.xml文件中,然后通过@String/xxx取得对应的字符串内容的;
5)textColor:设置字体颜色;
6)textStyle:设置字体风格,三个可选值:normal(无效果),bold(加粗),italic(斜体);
7)textSize:字体大小,单位sp;
8)background:控件的背景颜色,可以理解为填充整个控件的颜色,可以是图片哦。
3,带阴影的TextView
1)涉及到的4个属性:
android:shadowColor:设置阴影颜色,需要与shadowRadius一起使用;
android:shadowRadius:设置阴影的模糊程度,设为0.1就变成字体颜色了;
android:shadowDx:设置阴影在水平方向的偏移,就是水平方向阴影开始的横坐标位置;
android:shadowDy:设置阴影在竖直方向的偏移,就是竖直方向阴影开始的纵坐标位置。
简单示例代码和效果:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context="com.example.luolu.textviewdemo.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:shadowColor="#181717"
    android:shadowDx="10.0"
    android:shadowDy="15.0"
    android:shadowRadius="5.0"
    android:text="带阴影的TextView"
    android:textColor="#070721"
    android:textSize="48sp" />

</RelativeLayout>


image.png

3,带边框的TextView
1)涉及到的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时,从左到右,然后逆时针方向转,当angle = 90度时从下往上 type:设置渐变的类型。
简单示例代码和效果:
drawable -->r1.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:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp"/>
</shape>

drawable -->r2.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>

Layout -->activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context="com.example.luolu.textviewdemo.MainActivity">

<TextView
    android:id="@+id/txtOne"
    android:layout_width="250dp"
    android:layout_height="64dp"
    android:textSize="20sp"
    android:gravity="center"
    android:background="@drawable/r1"
    android:text="矩形边框的TextView" />

<TextView
    android:id="@+id/txtTwo"
    android:layout_width="250dp"
    android:layout_height="64dp"
    android:layout_marginTop="30dp"
    android:textSize="20sp"
    android:gravity="center"
    android:background="@drawable/r2"
    android:text="圆角边框的TextView" />

</LinearLayout>
效果图:


image.png

4,带图片的TextView
1)基本用法:
设置图片的核心其实就是:drawableXxx;可以设置四个方向的图片: drawableTop(上),drawableButtom(下),drawableLeft(左),drawableRight(右) 另外,也可以使用drawablePadding来设置图片与文字间的间距;
实例代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context="com.example.luolu.textviewdemo.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:drawableTop="@drawable/luolu1"
    android:drawableLeft="@drawable/luolu2"
    android:drawableRight="@drawable/luolu3"
    android:drawableBottom="@drawable/luolu4"
    android:drawablePadding="10dp"
    android:text="猜猜这是谁?"
    android:textSize="26sp"/>

</LinearLayout>
效果图:


image.png

5,跑马灯效果的TextView
实例实现代码:
<TextView
android:id="@+id/txtOne"
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="Alpha,未来已来!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!haha。。。。。。。呵呵呵呵呵,呵呵呵呵呵gggggggggggggggg呵呵呵呵呵~"/>
效果图:


image.png

6,设置TextView字间距和行间距
字间距:
android:textScaleX:控制字体水平方向的缩放,默认值1.0f,值是float
Java中setScaleX(2.0f);
行间距: Android系统中TextView默认显示中文时会比较紧凑,为了让每行保持的行间距
android:lineSpacingExtra:设置行间距,如"3dp" android:lineSpacingMultiplier:设置行间距的倍数,如"1.2"

7,自动换行
自动换行通过 android:singleLine 设置,默认为 false。
如需要自动换行,可以用:
android:singleLine = "false"
如果要在一行显示完,不换行,可以用:
android:singleLine = "true"
如果多行显示不完,添加maxLines的属性即可。

相关文章

网友评论

      本文标题:TextView知识点总结和demo实现

      本文链接:https://www.haomeiwen.com/subject/gqecvxtx.html