安卓文字过长,跑马灯文字滚动效果
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: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: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" />
然后就成功了!!
更多相关文章
- Android五大布局和ConstraintLayout
- android:hintText与android:inputType详解
- Android(安卓)API教程:人脸检测(Face Detect)
- Android(安卓)上层界面到内核代码的完整的流程分析,以alarm为例子
- 解决android listview中OnItemClickListener事件和里面button点
- Android(安卓)控制EditText输入为英文或数字
- Android的webView和h5+js交互
- Android:改变 Toolbar 的文字和溢出图标颜色
- android 反汇编,修改,重新打包