1.TextView

1. TextView

官方文档:

https://developer.android.google.cn/reference/android/widget/TextView

1.1 基础属性

属性    含义
layout_width    组件的宽度
layout_height    组件的高度
id    为TextView设置一个组件id
text    设置显示的文本内容
textColor    设置字体颜色
textStyle    设置字体风格:normal:无效果;bold:加粗;italic:斜体
textSize    字体大小,单位一般sp
background    控件的背景颜色,填充整个控件,可以是图片
gravity    设置控件中内容的对齐方式,TextView中是文字,ImageView中是图片

1.2 演示

<?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"
    >

    <TextView
        android:id="@+id/tv_1"
        android:layout_width="200dp"
        android:layout_height="400dp"
        android:text="DingJiaxiong"
        android:textColor="@color/black"
        android:textStyle="bold"
        android:textSize="30sp"
        android:background="#ff5621"
        android:gravity="center"
        />
    
</LinearLayout>


在这里插入图片描述


1.3 带阴影的TextView
属性    含义
shadowColor    设置阴影颜色,需要和shadowRadius一起使用
shadowRadius    设置阴影的模糊程度,0.1 为字体颜色,建议使用3.0
shadowDx    设置阴影在水平方向的偏移,水平方向阴影开始的横坐标位置
shadowDy    设置阴影在竖直方向的偏移,竖直方向阴影开始的纵坐标位置

 

android:shadowRadius="3.0"
android:shadowColor="#ff3523"
android:shadowDx="10"
android:shadowDy="20"

1.4 实现跑马灯效果的TextView
属性 含义
singleLine 内容单行显示
focusable 是否可以获取焦点
focusableInTouchMode 用于控制视图在触摸模式下是否可以聚焦
ellipsize 在哪里省略文本
marqueeRepeatLimit 字幕动画重复的次数

新建类MyTextView.java

package com.dingjiaxiong.mytextview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

@SuppressLint("AppCompatCustomView")
public class MyTextView extends TextView {

    public MyTextView(@NonNull Context context) {
        super(context);
    }

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

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

    @Override
    public boolean isFocused() {
        return true;
    }
}


xml中更改控件

<?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"
    >

    <com.dingjiaxiong.mytextview.MyTextView
        android:id="@+id/tv_1"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:text="DingJiaxiong DingJiaxiong DingJiaxiong DingJiaxiong DingJiaxiong"
        android:textColor="@color/black"
        android:textStyle="bold"
        android:textSize="30sp"
        android:gravity="center"

        android:shadowRadius="3.0"
        android:shadowColor="#ff3523"
        android:shadowDx="10"
        android:shadowDy="20"

        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:singleLine="true"
        android:focusable="true"
        android:focusableInTouchMode="true"
        />
    
    

</LinearLayout>

运行

在这里插入图片描述

在这里插入图片描述

这里获取焦点可加可不加,加了效果一样。

 

posted @ 2022-09-09 09:26  随遇而安==  阅读(12)  评论(0编辑  收藏  举报