在android中,如果设置了TextView控件为单行显示,且显示的文本太长的话,默认情况下会造成显示不全的情况,这种情况下我们需要设置该控件属性如下:

 <TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="@string/hello_world"
/>
    注意上述标记颜色的3个属性,按照上述设置这3个属性,此TextView空间就会滚动显示文本内容,保证了文本的完全显示。
    但是,上述方式只适用于界面中只有1个TextView的情况,当一个界面之中有多个TextView设置了相同属性,有且只有一个控件会滚动显示,其他TextView控件则不会滚动显示。
    造成这种情况的原因是TextView空间在滚动显示的时候必须获得焦点,但是默认情况下,只能有一个TextView空间获得焦点,所以造成上述情况。
    解决的方法很简单,重写类TextView的方法 isFocused(),使其总是返回true,即总是属于被选中的状态,这样就能够保证多个TextView控件都有滚动显示的效果。
    首先是继承TextView的类:
package com.example.administrator.test_hello_world;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
/**
* Created by Administrator on 2016/3/17.
* 实现跑马灯效果的TextView
*/
public class marqueeText extends TextView {
public marqueeText(Context context) {
super(context);
}
public marqueeText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public marqueeText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
//返回textview是否处在选中的状态
//而只有选中的textview才能够实现跑马灯效果
@Override
public boolean isFocused() {
return true;
}
}
    接下来是布局文件:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
>
<!--singleLine只能是单行显示-->
<com.example.administrator.test_hello_world.marqueeText
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="@string/hello_world"
/>
<com.example.administrator.test_hello_world.marqueeText
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="@string/hello_world"
/>
</LinearLayout>
最后是Activity:
MainActivity.java
package com.example.administrator.test_hello_world;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
void init_view() {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
init_view();
}
}
 

android TextView实现滚动显示效果的更多相关文章

  1. Android:TextView 自动滚动(跑马灯) (转)

    Android:TextView 自动滚动(跑马灯)       TextView实现文字滚动需要以下几个要点: 1.文字长度长于可显示范围:android:singleLine="true ...

  2. Android TextView 横向滚动(跑马灯效果)

    Android TextView 中当文字比較多时希望它横向滚动显示,以下是一种亲測可行的方法. 效果图: 1.自己定义TextView,重写isFocused()方法返回true,让自己定义Text ...

  3. android TextView 垂直滚动 用动画实现

    项目中需要让TextView 滚动一会显示一行一会显示一行 想了下用移动动画实现.但是实际中在整这个动画时调了好久.主要用到的动画方式是移动动画:TranslateAnimation 代码: PS: ...

  4. Android源码分析(十二)-----Android源码中如何自定义TextView实现滚动效果

    一:如何自定义TextView实现滚动效果 继承TextView基类 重写构造方法 修改isFocused()方法,获取焦点. /* * Copyright (C) 2015 The Android ...

  5. android TextView 带滚动条,和ScrollView 用法(暂时觉得ScrollView滑动速度比较快)

    本来是想做一个显示文字信息的,当文字很多时View的高度不能超过一个固定的值,当文字很少时View的高度小于那个固定值时,按View的高度显示.因为ScrollView没有maxHeight,无法满足 ...

  6. Android TextView 添加下划线的几种方式

    总结起来大概有5种做法:  1. 将要处理的文字写到一个资源文件,如string.xml(使用html用法格式化)   2. 当文字中出现URL.E-mail.电话号码等的时候,可以将TextView ...

  7. Android TextView图文混合编排

    Android TextView图文混合编排 实现技术细节不难,两个要点:1.html代码的混合编写.2,重写ImageGetter.例如:布局: <?xml version="1.0 ...

  8. android Textview动态设置大小

    import android.app.Activity; //import com.travelzen.tdx.BaseActivity; //import com.travelzen.tdx.uti ...

  9. Android TextView内容过长加省略号,点击显示全部内容

    在Android TextView中有个内容过长加省略号的属性,即ellipsize,用法如下: 在xml中:android:ellipsize="end"    省略号在结尾an ...

随机推荐

  1. java工程开发之图形化界面之(第一课)

    下面我们先上代码: package 一个事例图形小应用程序; import javax.swing.JApplet; import java.awt.Graphics; public class 绘制 ...

  2. ural1855 Trade Guilds of Erathia

    Trade Guilds of Erathia Time limit: 2.0 secondMemory limit: 64 MB The continent of Antagarich was co ...

  3. ListView下拉刷新、上拉载入更多之封装改进

    在Android中ListView下拉刷新.上拉载入更多示例一文中,Maxwin兄给出的控件比较强大,前面有详细介绍,但是有个不足就是,里面使用了一些资源文件,包括图片,String,layout,这 ...

  4. Android NDK 下的宽字符编码转换及icu库的使用(转)

    原贴http://topic.csdn.net/u/20101022/16/1b2e0cec-b9d2-42ea-8d9c-4f1bb8320a54.html?r=70149216 ,看过并动手实现, ...

  5. Android中的自定义Adapter(继承自BaseAdapter)——与系统Adapter的调用方法一致——含ViewHolder显示效率的优化(转)

    Android中很多地方使用的是适配器(Adapter)机制,那我们就要好好把这个Adapter利用起来,并且用出自己的特色,来符合我们自行设计的需要喽~~~ 下面先上一个例子,是使用ViewHold ...

  6. NDK常用命令

    NDK Build 用法(NDK Build)   1.ndk-build的用法 Android NDKr4引入了一个新的.小巧的shell脚本ndk-build,来简化源码编译. 该文件位于NDK根 ...

  7. gnu make

    http://stackoverflow.com/questions/448910/makefile-variable-assignment 更加全面的介绍 http://blog.csdn.net/ ...

  8. html5中的postMessage解决跨域问题

    解决跨域问题的方法有很多,如:图像ping(简单).jsonp(缺点是不能实现跨域post).CROS(CORS的本质让服务器通过新增响应头Access-Control-Allow-Origin,通过 ...

  9. zookeeper启动失败

    集群中3个节点,第一个started,第二个说Starting zookeeper ... already running as process xxxx,第三个说Starting zookeeper ...

  10. Gson通过借助TypeToken获取泛型参数的类型的方法

    最近在使用Google的Gson包进行Json和Java对象之间的转化,对于包含泛型的类的序列化和反序列化Gson也提供了很好的支持,感觉有点意思,就花时间研究了一下. 由于Java泛型的实现机制,使 ...