使用TextView实现跑马灯的效果
2024-09-07 09:19:36
1、定义textView标签的4个属性:
android:singleLine="true"//使其只能单行
android:ellipsize="marquee"//去掉省略号
android:focusable = "true"//使其循环
android : focusableInTouchMode = "true"
为了永久的滚动,应该再加一个android:marqueeRepeatLimit="marquee_forever",这样就能一直在进行
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.lj.chapter7.MainActivity"> <ImageButton
android:id="@+id/button_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp"
android:background="#00000000"
android:longClickable="false"
android:src="@drawable/button1" /> <com.example.lj.chapter7.MarqueeTextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button_one"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:text="@string/simpleText" /> <ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="59dp"
android:src="@drawable/image1" /> <com.example.lj.chapter7.MarqueeTextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:text="@string/simpleText" />
</RelativeLayout>
2、自定义一个继承TextView的类:
实现三个构造函数;
复写isfocued方法,返回true(默认都有有焦点,平常只有一个有焦点在第一行上)
package com.example.lj.chapter7; import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView; /**
* Created by LJ on 2016/8/13.
*/
public class MarqueeTextView extends TextView { public MarqueeTextView(Context context) {
super(context);
} public MarqueeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
} public MarqueeTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
} @Override
public boolean isFocused() {
return true;
}
}
3、使用自定义的类,方法是用包名和自定义类名代替TextView(src下的包名+类名)
参考:http://www.imooc.com/video/4308
最新文章
- C语言程序设计第6堂作业
- URL传值特殊字符处理
- 【131031】jsp学习实例 (2013-10-31 15:29:28)
- linux驱动初探之杂项设备(控制两个GPIO口)
- php面向对象Object
- ThinkPHP第十三天(CONF_PATH、APP_PATH,UEditor用法)
- 用Bash脚本将Linux普通用户添加为系统管理员
- Linux Shell : Test命令参数解析
- <;未来世界的幸存者>; 读后感(现实篇和职业篇)
- 【java学习笔记】文件操作
- mariadb 压缩包gz安装方式
- 开发中的Date处理
- 5、MyBatis-parameterType 入参封装 Map 流程
- [20180612]删除bootstrap$记录无法启动.txt
- 【AngularJS】解决ng-if中的ng-model值无效的问题(转)
- 查看内核页表kernel_page_tables (aarch32)
- Spring3 MVC请求参数获取的几种方法[转载]
- https 的理解
- Collapsing Margin:外边距叠加
- SQL修改字段默认值、获取字段默认值