Android开发 第三课 使用TextView实现跑马灯效果

来源:互联网 发布:dc 大事件 知乎 编辑:程序博客网 时间:2024/05/21 06:46

有时候由于文字过长,不能显示完整的文本。
marquee /mɑːˈkiː/
1.
N-COUNT A marquee is a cover over the entrance of a building, for example a hotel or a theatre. (饭店或剧院等楼房入口的)遮蓬; 遮檐
2.
N-COUNT A marquee is a large tent which is used at a fair, garden party, or other outdoor event, usually for eating and drinking in. (户外活动时作为餐饮场所的)大帐篷

<?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"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.adb.li806.deom1.MainActivity"><!--这种方法只适合单个textView 因为textView默认第一个TextView拿到焦点而后面的TextView没有拿到焦点但是我们重新定义的com.adb.li806.deom1.MarqueeText设置每个com.adb.li806.deom1.MarqueeText都能拿到焦点,这样就实现了跑马灯的效果-->    <com.adb.li806.deom1.MarqueeText        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:singleLine="true"        android:text="@string/hello_world"        android:textSize="20sp"        tools:layout_editor_absoluteX="11dp"        tools:layout_editor_absoluteY="6dp"        android:ellipsize="marquee"        android:focusable="true"        android:focusableInTouchMode="true"        />    <!--    距离:px 像素值(不建议使用,在开发中不同分辨率不能进行等比的拖放)         dp/dip 可以根据不同分辨率进行等比的拖放         dp推荐使用(标准)         sp  可以根据不同分辨率进行等比的拖放,推荐显示文字时使用    -->    <com.adb.li806.deom1.MarqueeText        android:id="@+id/textView2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:singleLine="true"        android:text="@string/hello_world"        android:textSize="20sp"        tools:layout_editor_absoluteX="16dp"        tools:layout_editor_absoluteY="46dp"        android:ellipsize="marquee"        android:focusable="true"        android:focusableInTouchMode="true"        android:layout_marginTop="13dp"        android:layout_below="@+id/textView1"        android:layout_centerHorizontal="true" /></RelativeLayout>
<resources>    <string name="app_name">Deom1</string>    <string name="hello_world">我是一个TextView,我是一个TextView,我是一个TextView,我是一个TextView    ,我是一个TextView,我是一个TextView</string></resources>
跑马灯的类package com.adb.li806.deom1;import android.content.Context;import android.support.annotation.Nullable;import android.util.AttributeSet;import android.widget.TextView;/** * Created by li806 on 2017/8/5. */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(){        //来判断当前的TextView是不是在一个被选中的状态上。        return true;    }}

这里写图片描述

原创粉丝点击