Android 高级UI设计笔记05:使用TextView实现跑马灯的效果
1. 使用TextView属性实现跑马灯的效果:
(1). 新建一个Android工程,命名为"MarqueeTextViewDemo",如下:

(2). 来到activity_main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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.himi.marqueetextviewdemo.MainActivity" > <TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:text="@string/hello_world" /> </RelativeLayout>
(3). 来到MainActivity.java,如下:
package com.himi.marqueetextviewdemo; import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} }
(4). 布署程序到模拟器上,如下:
效果如下:

(5). 总结:
定义TextView标签的4个属性:
android:singleLine="true"//使其只能单行
android:ellipsize="marquee"//是否滚动
android:focusable = "true"// 设置是否获得焦点
android:focusableInTouchMode = "true"//设置在Touch模式下View是否能取得焦点
设置上面属性之后,此时TextView显示文字就会自动循环显示。但是,对于一个大View中有很多子View来说,同一时刻只能有一个子View获得focus!也就是说当前这一屏上,最多只能有一个view能有跑马灯效果,而不能多个View同时都有跑马灯效果。
2. 自定义类MarqueeTextView继承自TextView实现跑马灯的效果:
(1)首先来到了activity_main.xml布局文件,如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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.himi.marqueetextviewdemo.MainActivity" > <com.himi.marqueetextviewdemo.MarqueeTextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:singleLine="true"
android:text="@string/hello_world" />
<com.himi.marqueetextviewdemo.MarqueeTextView
android:id="@+id/textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:singleLine="true"
android:layout_marginTop="15dp"
android:text="@string/hello_world" /> </RelativeLayout>
(2)来到了MarqueeTextView继承自TextView,如下:
package com.himi.marqueetextviewdemo; import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView; public class MarqueeTextView extends TextView { public MarqueeTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO 自动生成的构造函数存根
} public MarqueeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO 自动生成的构造函数存根
} public MarqueeTextView(Context context) {
super(context);
// TODO 自动生成的构造函数存根
} /**
* 复写isfocued方法,返回true(默认都是有焦点,平常只有一个有焦点在第一行上)
* 这样才能确保每一个MarqueeTextView都具备焦点
*/
@Override
public boolean isFocused() {
// TODO 自动生成的方法存根
return true;
} }
(3)来到了MainActivity.java:如下
package com.himi.marqueetextviewdemo; import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} }
(4)运行效果,如下:

总结:
-->1. 自定义类MarqueeTextView继承自TextView
• 实现三个构造函数;
• 复写isfocued方法,返回true(默认都有有焦点)-----> 实现多个View同时都有跑马灯效果,每个使用的MarqueeTextView都要焦点。
平常只有一个有焦点在第一行上。
-->2. 使用自定义的类,方法是用包名和自定义类名代替TextView(src下的包名+类名)
3. Android系统中TextView实现跑马灯效果,必须具备以下几个条件:
- android:ellipsize="marquee"
- TextView必须单行显示,即内容必须超出TextView大小
- TextView要获得焦点才能滚动
Android 高级UI设计笔记05:使用TextView实现跑马灯的效果的更多相关文章
- Android 高级UI设计笔记07:RecyclerView 的详解
1. 使用RecyclerView 在 Android 应用程序中列表是一个非常重要的控件,适用场合非常多,如新闻列表.应用列表.消息列表等等,但是从Android 一出生到现在并没有非常 ...
- Android 高级UI设计笔记14:Gallery(画廊控件)之 3D图片浏览
1. 利用Gallery组件实现 3D图片浏览器的功能,如下: 2. 下面是详细的实现过程如下: (1)这里我是测试性代码,我的图片是自己添加到res/drawable/目录下的,如下: 但是开发中不 ...
- Android 高级UI设计笔记22:Android 指示引导页(带圆点)
1. 引导页: 我们在安装某个软件首次运行时,大部分都会有一个引导页的提示,介绍软件新功能的加入或者使用说明等,支持滑动且下面会有几个圆点,显示共有多少页和当前图片的位置,类似如下效果: 2. 引导页 ...
- Android 高级UI设计笔记06:仿微信图片选择器(转载)
仿微信图片选择器: 一.项目整体分析: 1. Android加载图片的3个目标: (1)尽可能的去避免内存溢出. a. 根据图片的显示大小去压缩图片 b. 使用缓存对我们图片进行管理(LruCache ...
- Android 高级UI设计笔记21:Android SegmentView(分段选择控件)
1. 分段控制(SegmentView) 首先我们先看看什么是SegmentView的效果,如下: 分段控制这个View控件是ios7的分段控制,和QQ消息页面顶部的效果一样,android没有这个控 ...
- Android 高级UI设计笔记19:PopupWindow使用详解
1. PopupWindow使用 PopupWindow这个类用来实现一个弹出框,可以使用任意布局的View作为其内容,这个弹出框是悬浮在当前activity之上的. 2. PopupWindow使用 ...
- Android 高级UI设计笔记02:可以拖动交换item位置的GridView(转载)
如果大家不知道GridView基本使用,可以先参见:Android(java)学习笔记154:使用GridView以及重写BaseAdapter 1. 首先我们明白GridView拖拽的思路: ()根 ...
- Android 高级UI设计笔记20:RecyclerView 的详解之RecyclerView添加Item点击事件
1. 引言: RecyclerView侧重的是布局的灵活性,虽说可以替代ListView但是连基本的点击事件都没有,这篇文章就来详细讲解如何为RecyclerView的item添加点击事件,顺便复习一 ...
- Android 高级UI设计笔记17:Android在非UI线程中显示Toast
1. 子线程的Toast怎么显示不出来? 因为Toast在创建的时候会依赖于一个Handler,并且一个Handler是需要有一个Looper才能够创建,而普通的线程是不会自动去创建一个Looper对 ...
随机推荐
- Activating Google Cloud Storage
先决条件 你需要下面的内容: 1.一个Google账户,比如来自Gmail.如果你没有,请在Google account signup site注册. 2.一个新的或已经存在的Google Devel ...
- 王家林 Spark公开课大讲坛第一期:Spark把云计算大数据速度提高100倍以上
王家林 Spark公开课大讲坛第一期:Spark把云计算大数据速度提高100倍以上 http://edu.51cto.com/lesson/id-30815.html Spark实战高手之路 系列书籍 ...
- VS2015创建的C++程序在Debug模式下不能调试
如题,不能调试的问题,解决办法: 1.修改工程属性页 2.修改VS2015托管兼容模式 工具->选项->调试,勾选使用托管兼容
- 优秀java开源项目与解决方案推荐与概论
http://www.oschina.net/project/lang/19/java http://www.open-open.com/ http://java-source.net/ https: ...
- mark标签:
mark元素表示页面中需要突出或高亮显示的内容,在搜索结果中也常常出现,比如检索结果中的关键词高亮显示. 案例:[html]<!DOCTYPE HTML><html> & ...
- 【转】iOS开发工具系列(按功能分)
http://www.cocoachina.com/newbie/basic/2014/0417/8187.html 这是我们多篇iOS开发工具系列篇中的一篇,此前的文章比如:那些不能错过的Xcode ...
- POJ 3169 Layout (差分约束系统)
Layout 题目链接: Rhttp://acm.hust.edu.cn/vjudge/contest/122685#problem/S Description Like everyone else, ...
- uva 10274 Fans and Gems(隐式图搜索+模拟)
Fans and Gems Input: Standard Input Output: Standard Output Tomy's fond of a game called 'Fans and G ...
- 在线教育服务:http://www.ablesky.com/
在线教育服务:http://www.ablesky.com/
- CoordinatorLayout的简单应用(材料设计新控件)
CoordinatorLayout字面意思为:协调布局,一般作为根布局使用.关于这个布局,记录一下两个用法,备忘. 一.配合 FloatingActionBar 使用 <?xml version ...