TextView控件

基础属性

  • layout_width,组件的高度
    • match_parent宽度取它外面(父容器)的宽度
    • wrap_content根据控件内部的内容来自动分配它的宽度
    • 直接写数值(不推荐)
  • layout_height,组件的高度
  • id,组件的ID
  • text,文本内容
  • textColor设置字体颜色(有八位,十六进制,两位一起,透明度,RGB
  • textStyle,设置字体风格,三个可选项,normal(无效果),bold(加粗),italic(斜体)
  • textSize,字体大小,单位一般sp
  • background,组件的背景
  • gravity,设置组件中内容的对齐方向,TextView是文字,ImageView是图片等

XML里的文字和颜色值都要写在res里规范

这是为了适配,不同类型的,不同风格,只需要几个XML即可,无需重复开发。

带阴影的TextView

android:shadowColor,设置阴影颜色,需要与shadowRadius一起使用

android:shadowRadius,设置阴影的模糊程度,设为0.1就变成了字体颜色了,建议使用3.0

android:shadowDx,设置阴影在水平方向的偏移,即水平方向阴影开始的横坐标

android:shadowDy,同上

实现跑马灯效果的TextView

android:singleLine,设置内容为单行显示,已弃用,用lines=“1”,但是用singleLine可以,lines跑不起来

android:focusable,是否可以获取焦点

android:focusableInTouchMode,用于控制在触摸模式下是否可以聚焦

android:ellipsize,在哪里省略文本,用marquee

android:marqueeRepeatLimit,字母动画重复的次数

如何让他跑起来

  • 设置处理方式,可点击clickable
  • 使用自定义的view,继承自TextView类,实现前三个构造方法,调用isfocus,并返回True。将XML改为自定义的类
  • 在标签内使用<requestFocus/>

主要思想,字有多列或多行,获取焦点的能力,程序内提供焦点,还是交互的焦点,三种提供焦点的方法

    <com.example.android003.Mypamadeng
        android:layout_width="match_parent"
        android:layout_height="103dp"
        android:gravity="center_vertical"
        android:shadowColor="@color/red"
        android:shadowDx="10"
        android:shadowDy="10"
        android:shadowRadius="3"
        android:text="@string/jieke"
        android:textColor="@color/blue"
        android:textSize="@android:dimen/app_icon_size"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:focusableInTouchMode="true">
    </com.example.android003.Mypamadeng>

猜你喜欢

转载自blog.csdn.net/weixin_62302176/article/details/132613399