Android入门学习——TextView

TextView

  • 基本属性
  1. layout width:组件的宽度。
    android:layout_width="match_parent"与父容器相同
    android:layout_width="wrap_content"随内容而变
    android:layout_width="xxdp"自定义
  2. layout_height:组件的高度 。
    android:layout_height="match_parent"与父容器相同
    android:layout_height="wrap_content"随内容而变
    android:layout_height="xxdp"自定义
  3. id:为TextViewi设置一个组件id 。
    android:id="@+id/xxx"用于控件绑定
  4. text:设置显示的文本内容 。
    android:text="xxx"TextView的显示内容
  5. textColor:设置字体颜色
    android:textColor="#FFFFFFFF"每二位一组分别对应透明度、红、绿、蓝。
    规范使用方式在values下的colors.xml中添加<color name="colorWhite">#FFFFFFFF</color>
    android:textColor="@color/colorWhite"
  6. textStyle:设置字体风格,三个可选值:normal(无效果),bold加粗),italic(斜体) 。
    android:textStyle=xxx""
  7. textSize:字体大小,单位一般是用sp 。
    android:textSize="xxxsp"
  8. background:控件的背景颜色,可以理解为填充整个控件的颜色,可以是图片。
    android:background="@color/xxx"
    android:background="@drawable/xxx"
  9. gravity:设置控件中内容的对齐方向,TextView中是文字,ImageView中是图片等等。
    android:gravity="xxx"控件内容的对齐方向
    android:layout_gravity="xxx"整个控件与父容器的对齐方向

拓展

  • 带阴影的TextView

1.android:shadowColor="@color/xxx"设置阴影颜色,需要与shadowRadius-一起使用
2.android:shadowRadius=""设置阴影的模糊程度,设为0.1就变成字体颜色了,建议使用3.0
3.android:shadowDx=""设置阴影在水平方向的偏移,就是水平方向阴影开始的横坐标位置
4. android:shadowDy=""设置阴影在竖直方向的偏移,就是竖直方向阴影开始的纵坐标位置

  • TextView的跑马灯效果

text内容一般要长于容器宽度

1.android:singleLine="true"内容单行显示
2.android:focusable="true"是否可以获取焦点
3.android:focusablelnTouchMode="true"用于控制视图在触摸模式下是否可以聚焦
4.android:ellipsize="marquee"跑马灯效果
5.android:marqueeRepeatLimit="marquee_forever"跑马灯动画重复的次数
6. <requestFocus/>获取焦点,也可以通过自定义View重写isFocused()函数为return true;实现

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" >
        <requestFocus/>
        </TextView>

</androidx.constraintlayout.widget.ConstraintLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值