一、TextView
基本属性
id:设置唯一标识 例:android:id="@+id/tv_one"
gravity:设置文本显示效果; 例:center_vertical(水平居中)
textColor:文本颜色;例:android:textColor="@color/black"
textSize:字体大小,通常使用 15sp,以sp和位置控制的dp区分;例:android:textSize="30sp"
layout_width,layout_height:控件的长度宽度属性;例子:android:layout_width="match_parent" 和 android:layout_height="200dp"
//设置阴影显示
shadowColor:阴影颜色;例:android:shadowColor="@color/red" 或者 android:shadowColor="#FFFF0000"
shadowRadius:必须和 shadowColor 搭配使用,缺一不可;例:android:shadowRadius="3.0"
shadowDx,shadowDy:显示的偏移区域; 例:android:shadowDx="10.0" 和 android:shadowDy="10.0"
//跑马灯效果
文字长度过大时,使用跑马灯效果展示
singleLine:单行显示
ellipsize:省略号显示位置
marqueeRepeatLimit:
focusableInTouchMode:设置焦点
代码如下:(requestFocus启动获取焦点)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <?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" > <!--设置id和文本位置 设置阴影显示 设置显示及位置信息 设置跑马灯效果--> <!--跑马灯显示 方法1:android:focusable= "true" 方法2:实现继承TextView, 方法3:使用requestFocus设置焦点 --> <TextView android:id= "@+id/tv_one" android:gravity= "center_vertical" android:shadowColor= "@color/red" android:shadowDx= "10.0" android:shadowDy= "10.0" android:shadowRadius= "3.0" android:text= "@string/tv_one" android:textColor= "@color/black" android:textSize= "30sp" android:layout_width= "match_parent" android:layout_height= "200dp" android:singleLine= "true" android:ellipsize= "marquee" android:marqueeRepeatLimit= "marquee_forever" android:focusableInTouchMode= "true" > <requestFocus/> </TextView> </LinearLayout> |
代码如下:(继承TextView,进行方法的重写,在页面中的 TextView替换成 所声明继承的TextView控件 )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | package com.example.application; import android.content.Context; import android.util.AttributeSet; import androidx.annotation.Nullable; public class MyTextView extends androidx.appcompat.widget.AppCompatTextView { 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 ; } } |
分类:
Android Studio
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现