【Android】带阴影的TextView实现走马灯效果

您所在的位置:网站首页 focusable属性 【Android】带阴影的TextView实现走马灯效果

【Android】带阴影的TextView实现走马灯效果

2023-03-13 14:14| 来源: 网络整理| 查看: 265

TextView学习

基础属性:

名字含义layout_width组件宽度layout_height组件高度id为TextView设置一个组件idtext设置显示的文本内容textColor设置字体颜色(共八位,12代表透明度,34代表红色,56代表绿色,78代表蓝色)textStyle设置字体风格,三个可选值,normal(无效果),bold(加粗),italic(斜体)textSize字体大小,单位一般是用spbackgroup控件的背景颜色,可以理解为填充整个控件的颜色,可以是图片gravity设置控件中内容的对齐方向,TextView中是文字,ImageView中是图片match_parent填满上一级(父控件)容器wrap_content根据文字内容自动变长变短

代码演示

activity_main.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">TextViewandroid:id="@+id/tv_one"android:text="@string/tv_one"android:textColor="@color/black"android:textStyle="bold"android:textSize="30sp"android:background="@color/red"android:gravity="center"android:layout_width="200dp"android:layout_height="200dp"/>/LinearLayout>

color.xml

?xml version="1.0" encoding="utf-8"?>resources>color name="colorPrimary">#6200EE/color>color name="colorPrimaryDark">#3700B3/color>color name="colorAccent">#03DAC5/color>color name="black">#FF000000/color>color name="red">#FFFF0000/color>/resources>

strings.xml

resources>string name="app_name">demo/string>string name="tv_one">想学好安卓/string>/resources>

效果演示 ![请添加图片描述](https://img-blog.csdnimg.cn/6b2f2090448744f3bf7b1d34e7a72c51.gif

带阴影的TextView

名字含义android:shadowColor阴影的颜色android:shadowDx水平方向上的偏移量android:shadowDy垂直方向上的偏移量android:shadowRadius是阴影的的半径大小

实现跑马灯效果的TextView

名字含义android:singleLine内容单行显示android:focusable是否可以获取焦点android:focusableInTouchMode用于控制视图在触摸模式下是否可以聚焦android:ellipsize在哪里省略文本android:marqueeRepeatLimit字幕动画重复的次数

实现代码

?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">TextViewandroid:id="@+id/tv_one"android:layout_width="match_parent"android:layout_height="200dp"android:ellipsize="marquee"android:focusable="true"android:focusableInTouchMode="true"android:gravity="center_vertical"android:marqueeRepeatLimit="marquee_forever"android:clickable="true"android:shadowColor="@color/red"android:shadowDx="10.0"android:shadowDy="10.0"android:shadowRadius="3.0"android:singleLine="true"android:text="@string/tv_one"android:textColor="@color/black"android:textSize="30sp"android:textStyle="italic" />/LinearLayout>

效果演示 请添加图片描述

这是需要点击之后才会出现走马灯的效果,如果需要运行之后自动出现走马灯效果那么加上一行代码

完整代码如下,运行之后就会自动的出现走马灯效果

?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">TextViewandroid:id="@+id/tv_one"android:layout_width="match_parent"android:layout_height="200dp"android:ellipsize="marquee"android:focusable="true"android:focusableInTouchMode="true"android:gravity="center_vertical"android:marqueeRepeatLimit="marquee_forever"android:clickable="true"android:shadowColor="@color/red"android:shadowDx="10.0"android:shadowDy="10.0"android:shadowRadius="3.0"android:singleLine="true"android:text="@string/tv_one"android:textColor="@color/black"android:textSize="30sp"android:textStyle="italic" >requestFocus/>/TextView>/LinearLayout>

方法二:自定义TextView实现走马灯效果

MyTextView.java

package com.c201801090107.demo;import android.content.Context;import android.util.AttributeSet;import android.widget.TextView;import androidx.annotation.Nullable;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);}@Overridepublic boolean isFocused() {return true;}}

activity_main.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.c201801090107.demo.MyTextViewandroid:id="@+id/tv_one"android:layout_width="match_parent"android:layout_height="200dp"android:ellipsize="marquee"android:focusable="true"android:focusableInTouchMode="true"android:gravity="center_vertical"android:marqueeRepeatLimit="marquee_forever"android:clickable="true"android:shadowColor="@color/red"android:shadowDx="10.0"android:shadowDy="10.0"android:shadowRadius="3.0"android:singleLine="true"android:text="@string/tv_one"android:textColor="@color/black"android:textSize="30sp"android:textStyle="italic" >requestFocus/>/com.c201801090107.demo.MyTextView>/LinearLayout>

运行直接启动,无需点击 请添加图片描述

一个人必须不停的努力,才能不被茫茫人海淹没


【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3