文章目录
所谓跑马灯 就是 文本框里面的 文字超过了 ,文本框的 宽度 ,怎轮播显示。
1、在代码里 代码动态设计 (第一种)
1)xml 文件
<TextView
android:id="@+id/text_id"
android:layout_width="100dp"
android:layout_height="30dp"
android:text="Hello World! wertyuiodfghjklxcvbnm,.dfghjklghjkl"
/>
2) 功能文件
textView = (TextView) findViewById(R.id.text_id);
textView.setMarqueeRepeatLimit(Integer.MAX_VALUE);
textView.setFocusable(true);
textView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
textView.setSingleLine();
textView.setFocusableInTouchMode(true);
textView.setHorizontallyScrolling(true);
2、在 xml 文件 里配置 跑马灯 (第2种)
<TextView
android:layout_marginTop="30dp"
android:layout_width="100dp"
android:layout_height="30dp"
android:text="Hello World! wertyuiodfghjklxcvbnm,.dfghjklghjkl"
android:singleLine="true"
android:scrollHorizontally="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
/>
3、实现一个 Activity 多个TextView 跑马灯效果
如果在同一个activity里需要使多 个textivew单行滚动显示,都设置了以上属性,系统不知道到底哪个textview获取了焦点,会造成冲突,只能有一个TextView 实现跑马灯。
如下:
所以,就得单独写一个MarqueeTextView extends TextView的方法,使每一个需要滚动显示的textview都要focusable为true,下面是继承TextView的MarqueeTextView类文件
1) 自定义的 MarqueeTextView 类
package com.example.lum.mytestword;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
/**
* Created by lum on 2019/1/15.
*/
@SuppressLint("AppCompatCustomView")
public class MarqueeTextView extends TextView {
public MarqueeTextView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MarqueeTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public MarqueeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
public boolean isFocused() {
// TODO Auto-generated method stub
return true;
}
}
2)、 xml 文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.lum.mytestword.MainActivity">
<com.example.lum.mytestword.MarqueeTextView
android:id="@+id/text_id"
android:layout_width="100dp"
android:layout_height="30dp"
android:text="Hello World! wertyuiodfghjklxcvbnm,.dfghjklghjkl"
/>
<com.example.lum.mytestword.MarqueeTextView
android:layout_marginTop="30dp"
android:id="@+id/text_two"
android:layout_width="100dp"
android:layout_height="30dp"
android:textColor="@color/colorAccent"
android:text="Hello World! wertyuiodfghjklxcvbnm,.dfghjklghjkl"
/>
<com.example.lum.mytestword.MarqueeTextView
android:layout_marginTop="30dp"
android:layout_width="100dp"
android:layout_height="30dp"
android:textColor="@color/colorPrimary"
android:text="Hello World! wertyuiodfghjklxcvbnm,.dfghjklghjkl"
android:singleLine="true"
android:scrollHorizontally="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
/>
</LinearLayout>
3)、功能文件 MainActivity
package com.example.lum.mytestword;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView textView,textViewTwo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.text_id);
textView.setMarqueeRepeatLimit(Integer.MAX_VALUE);
textView.setFocusable(true);
textView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
textView.setSingleLine();
textView.setFocusableInTouchMode(true);
textView.setHorizontallyScrolling(true);
textViewTwo = (TextView) findViewById(R.id.text_two);
textViewTwo.setMarqueeRepeatLimit(Integer.MAX_VALUE);
textViewTwo.setFocusable(true);
textViewTwo.setEllipsize(TextUtils.TruncateAt.MARQUEE);
textViewTwo.setSingleLine();
textViewTwo.setFocusableInTouchMode(true);
textViewTwo.setHorizontallyScrolling(true);
}
}
效果展示: