发布于 2016-06-17 00:35:04 | 130 次阅读 | 评论: 0 | 来源: 网友投递

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

Android移动端操作系统

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


这篇文章主要介绍了Android TextView跑马灯效果实现方法,涉及Android布局文件中相关属性的设置技巧,非常简单实用,需要的朋友可以参考下

本文为大家分享一个非常简单但又很常用的控件,跑马灯状态的TextView。当要显示的文本长度太长,又不想换行时用它来显示文本,一来可以完全的显示出文本,二来效果也挺酷,实现起来超级简单,所以,何乐不为。先看下效果图:

代码实现

TextView自带了跑马灯功能,只要把它的ellipsize属性设置为marquee就可以了。但有个前提,就是TextView要处于被选中状态才能有效果,看到这,我们就很自然的自定义一个控件,写出以下代码:


public class MarqueeTextView extends TextView {

 public MarqueeTextView(Context con) {
  super(con);
 }

 public MarqueeTextView(Context context, AttributeSet attrs) {
  super(context, attrs);
 }

 public MarqueeTextView(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
 }

 @Override
 public boolean isFocused() {
  // TODO Auto-generated method stub
  if(getEditableText().equals(TruncateAt.MARQUEE)){
   return true;
  }
  return super.isFocused();
 }
}

重写了isFocused方法,并进行判断,只有设置了marqueen属性的才保持选中状态,否则它就跟普通TextView一样。接下来就可以直接使用了,看下布局:


<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">
 <FrameLayout
  android:id="@+id/titlebar_layout"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="#39ac69" >
  <LinearLayout

   android:layout_width="match_parent"
   android:layout_height="50dp"
   android:background="#ffffff"
   android:gravity="center_vertical"
   android:orientation="horizontal" >

   <ImageView
    android:id="@+id/home_location_iv"
    android:layout_width="25dp"
    android:layout_height="27dp"
    android:layout_marginLeft="10dp"
    android:scaleType="fitXY"
    android:src="@drawable/icon_place" />

   <com.lxj.marqueetextview.MarqueeTextView
    android:id="@+id/home_location_tv"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_weight="1"
    android:ellipsize="marquee"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:gravity="center"
    android:marqueeRepeatLimit="marquee_forever"
    android:scrollHorizontally="true"
    android:singleLine="true"
    android:text="正在定位..."
    android:textColor="#39ac69"
    android:textSize="18sp" />

   <ImageView
    android:id="@+id/home_search_iv"
    android:layout_width="25dp"
    android:layout_height="27dp"
    android:layout_marginRight="10dp"
    android:scaleType="fitXY"
    android:src="@drawable/icon_place" />
  </LinearLayout>
</FrameLayout>
</LinearLayout>

要注意两点ellipsize属性要设置为”marquee”,行数属性即singleLine要设置为true。到此TextView的跑马灯效果就实现了。

希望本文对大家学习Android软件编程有所帮助。



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

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