发布于 2017-09-24 10:56:50 | 157 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要为大家详细介绍了android TextView实现跑马灯效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了android TextView跑马灯效果的具体代码,供大家参考,具体内容如下

一、要点

设置四个属性

android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"

直接在xml中使用


<TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:singleLine="true"
  android:ellipsize="marquee"
  android:focusable="true"
  android:focusableInTouchMode="true"
  android:text="人生是一场无休、无歇、无情的战斗,凡是要做个够得上称为人的人,都得时时向无形的敌人作战。" />


注意:singleLine属性 不能换成 maxlLines 

二、复杂布局

在复杂的布局中可能不会实现跑马灯效果。例如如下布局中,就只有第一个TextView会有跑马灯效果


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/tv1"
  android:singleLine="true"
  android:ellipsize="marquee"
  android:focusable="true"
  android:focusableInTouchMode="true"
  android:text="人生是一场无休、无歇、无情的战斗,凡是要做个够得上称为人的人,都得时时向无形的敌人作战。" />
 <TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_below="@+id/tv1"
  android:layout_marginTop="10dp"
  android:singleLine="true"
  android:ellipsize="marquee"
  android:focusable="true"
  android:focusableInTouchMode="true"
  android:text="人生是一场无休、无歇、无情的战斗,凡是要做个够得上称为人的人,都得时时向无形的敌人作战。" />


</RelativeLayout>

这时候就需要自定义View,实现跑马灯效果

自定义MarQueeTextView extents TextView  重写isFocused()方法,返回true


public class MarqueeText extends TextView {
 public MarqueeText(Context context) {
  super(context);
 }

 public MarqueeText(Context context, @Nullable AttributeSet attrs) {
  super(context, attrs);
 }

 public MarqueeText(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
 }

 @Override
 public boolean isFocused() {
  return true;
 }
}

布局中使用


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <com.example.dhj.marqueedemo.View.MarqueeText
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/tv1"
  android:singleLine="true"
  android:ellipsize="marquee"
  android:focusable="true"
  android:focusableInTouchMode="true"
  android:text="人生是一场无休、无歇、无情的战斗,凡是要做个够得上称为人的人,都得时时向无形的敌人作战。" />
 <com.example.dhj.marqueedemo.View.MarqueeText
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_below="@+id/tv1"
  android:layout_marginTop="10dp"
  android:singleLine="true"
  android:ellipsize="marquee"
  android:focusable="true"
  android:focusableInTouchMode="true"
  android:text="人生是一场无休、无歇、无情的战斗,凡是要做个够得上称为人的人,都得时时向无形的敌人作战。" />


</RelativeLayout>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持PHPERZ。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务