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

有时候由于文字过长,不能显示完整的文本。
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;
    }
}

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值