android:singleLine="true"

android:ellipsize="marquee"

android:focusable="true"

android:focusableInTouchMode="true"

当文字过长超过手机屏幕大小的时候可以简单的设置文字跑马灯形式流动(但只支持一段文本文字,若有两段或者多段重复使用就没有如此效果,方法如下)


新建一个java类文件,命名为MarqueeTextView,继承安卓的TextView,

alt+insert选择override Methods,然后加入TextView本身的全部方法,并在外部重写 isFocused方法,return true,

最后将.xml里面用到跑马灯效果的TextView全改成该文件的位置,具体代码如下:


MarqueeTextView.java

package com.hannah.work.marquee;


import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

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

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

    public MarqueeTextView(Context context) {
        super(context);

    }

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


activity_main.xml

(里面的TextView全改为java文件位置)

        android:id="@+id/textView1"
        android:text="@string/hello_world1"
        android:layout_width="match_parent"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:layout_height="wrap_content" />




            android:id="@+id/textView2"
        android:text="@string/hello_world2"
        android:layout_width="match_parent"
        android:layout_marginTop="25dp"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:layout_height="wrap_content" />


然后就成功了!!





更多相关文章

  1. Android五大布局和ConstraintLayout
  2. android:hintText与android:inputType详解
  3. Android(安卓)API教程:人脸检测(Face Detect)
  4. Android(安卓)上层界面到内核代码的完整的流程分析,以alarm为例子
  5. 解决android listview中OnItemClickListener事件和里面button点
  6. Android(安卓)控制EditText输入为英文或数字
  7. Android的webView和h5+js交互
  8. Android:改变 Toolbar 的文字和溢出图标颜色
  9. android 反汇编,修改,重新打包

随机推荐

  1. Android(安卓)中Popwindow弹出菜单的两种
  2. [置顶] 我的Android进阶之旅------>Andro
  3. android 使用Intent传递数据之静态变量
  4. AndroidManifest.xml学习
  5. Android中图像和图像处理
  6. Android(安卓)写一个属于自己的Rxjava(二)
  7. Android最佳实践之高效的应用导航
  8. android 值得学习的网站
  9. android切换屏幕时的生命周期
  10. Android中Activity启动模式详解