Android基础:TextView文本控件

讲解

        TextView作为Android最基础也是最常用的组件之一,他承担着文本的显示重任。要注意,其显示的文本内容是无法在界面直接被用户修改的。不过作为程序员,可以通过后台代码去修改TextView的内容和各项属性。另外要注意的是,TextView控件是需要放在容器里面的,比如LinearLayout容器(一般控件都要放在容器里面)。

基础属性介绍

属性说明
id设置一个组件id(唯一),通过findViewById()的方法获取到该对象,然后进行相关设置
layout_width设置组件宽度,可以填充数字和Android提供的枚举值,Android提供的两个枚举值:match_parent:与父类宽度匹配(减去padding)(在Level 8之后,代替废弃的fill_parent),wrap_content:组件应该足够大到足以其内容(加上padding,当然不超过其父类)。
layout_height设置组件高度,可以填充数字和Android提供的枚举值,Android提供的两个枚举值:match_parent:与父类高度匹配(减去padding)(在Level 8之后,代替废弃的fill_parent),wrap_content:组件应该足够大到足以其内容(加上padding,当然不超过其父类)。
text设置显示的文本内容
background设置背景颜色(或背景图片)
textColor设置字体颜色
textStyle设置字体样式 ,三个可选值:normal(无效果),bold(加粗),italic(斜体)
textSize字体大小,单位一般用sp
maxHeight设置文本框最大高度
gravity内容的对齐方向

        示例:

    <TextView
        android:id="@+id/tView1"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="修改"
        android:textColor="@color/white"
        android:textSize="90dp"
        android:textStyle="bold"
        android:gravity="center"
        android:background="@color/black"/>

后台调用

        Java后台通过ID调用。
        注意:Java会覆盖对应的TextView 原本内容。

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView tv = findViewById(R.id.tView1);
        tv.setText("TextView1");
    }

设置TextView的阴影

属性说明
shadowColor设置字体阴影的颜色,单独设置没有用,需要跟shadowRadius一起使用
shadowRadius设置字体阴影的模糊程度,一般设置3~5。
shadowDx设置阴影在水平方向的平移
shadowDy设置阴影在竖直方向的平移

        一般都是四个搭配使用。

    <TextView
        android:id="@+id/tv_one"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:gravity="center"
        android:shadowColor="@color/black"
        android:shadowRadius="3.0"
        android:shadowDx="8"
        android:shadowDy="8"
        android:text="@string/tv_one"
        android:textColor="@color/purple_500"
        android:textSize="20dp"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="0dp"
        tools:ignore="MissingConstraints" />

        效果图:
在这里插入图片描述

设置TextView边框

        首先要写Drawable文件:

        圆角带边框:

 <!-- ic_txt_radiuborder.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="15px"
        android:bottomRightRadius="15px"
        android:topLeftRadius="15px"
        android:topRightRadius="15px" />
    <!-- 设置一下边距,让空间大一点 -->
    <padding
        android:bottom="5dp"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp" />
</shape>

        矩形带边框:

 <!-- ic_txt_rectborder.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>

将TextView的blackground属性设置成上面这两个Drawable:


<?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"
android:background="#FFFFFF"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">

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

<TextView
    android:id="@+id/txtTwo"
    android:layout_width="100dp"
    android:layout_height="64dp"
    android:layout_marginTop="10dp"
    android:textSize="18sp"
    android:gravity="center"
    android:background="@drawable/ic_txt_radiuborder"
    android:text="圆角带边框的TextView" />
</LinearLayout>

在这里插入图片描述

带图片(drawableXxx)的TextView

        在日常生活中,我们经常会看到这种带图片和文字的模型,其实这里是可以通过drawableXxx实现:
在这里插入图片描述

属性说明
drawableTop图片显示在内容上方
drawableBottom图片显示在内容下方
drawableLeft图片显示在内容左侧
drawableRight图片显示在内容右侧
<?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"
android:background="#FFFFFF"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:drawableTop="@mipmap/ic_launcher"
    android:drawablePadding="10dp"
    android:gravity="center"
    android:text="安卓12.0" />

</LinearLayout>

在这里插入图片描述

实现跑马灯

属性说明
singleLine内容单行显示
focusable设置是否可以获取焦点 ,如果是跑马灯,则需要设置为true,让该textView去获取焦点
focusableTouchMode控制试图在触摸模式下是否可以聚焦,就是在触摸某个控件,会先将焦点移动到被触摸的控件上,然后需要再触摸该控件才会响应单击事件。
ellipsize设置省略文本. “start”:省略号显示在开头;“end”:省略号显示在结尾;“middle”:省略号显示在中间;“marquee”:以横向滚动方式显示(需要获取焦点)
marqueeRepeatLimit字母动画重复次数,如果要一直跑的话,那就要设置marquee_forever

        设置跑马灯,这些设置还不够,因为跑马灯是需要在有焦点的时候才能跑起来,让文本获取焦点主要有下面三种方式:

  1. 添加clickable=true 属性,这种只会在特殊情况下用,因为,这种需要你点击一下才能起效果
  2. 重写TextView的ifFocused方法,设置返回为True。然后使用你重写的文本框架。
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
import androidx.annotation.Nullable;
// @SuppressLint 解决:This custom view should extend androidx.appcompat.widget.AppCompatTextView instead
@SuppressLint("AppCompatCustomView")
public class MyTextView extends TextView {
    public MyTextView(Context context) {
        super(context);
    }

    public MyTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

	// 设置默认获取焦点
    @Override
    public boolean isFocused() {
        return true;
    }
}
<com.example.androidstudy.MyTextView
        android:id="@+id/tv_one"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:gravity="center"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="false"
        android:clickable="true"
        android:text="@string/tv_one"
        android:textColor="@color/purple_500"
        android:textSize="20dp"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="0dp">
    </com.example.androidstudy.MyTextView>
  1. 在<TextView> </TextView> 中加入 <requestFocus/>
  <TextView
        android:id="@+id/tv_one"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:gravity="center"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="false"
        android:clickable="true"
        android:text="@string/tv_one"
        android:textColor="@color/purple_500"
        android:textSize="20dp"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="0dp">
        <requestFocus/>
    </TextView>

        在这里要注意:android:singleLine 官方建议用maxLines 替换。但是maxLines折行算法有一些坑,可以参考这篇文章:关于TextView中maxLines替换singleLine遇到的坑

        关于TextView还有很多有意思的玩法,比如实现部分可点击,定制文本等。因为时间有限,在此就不一一描述,感兴趣的可以关注:2.3.1 TextView(文本框)详解。

  • 10
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值