1.长文字
当一个TextView的文字很长时,显示效果如下:
如果我们让它必须显示在一行时,加一条属性:
android:singleLine="true"
就变成这样:
然而我们看不到后面的文字了。。。
2.加一个属性:
android:ellipsize="marquee"
只是没了省略号,但是仍不能滚动。
3.再来:
再加两个属性:
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
发现可以滚动了。。。但是还没完
4.终极:
加两个TextView:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
/>
效果:
这种效果是由于只有上面一个会滚动,下面是不动的。
所以需要一个对付复杂布局的方法:
1.新建一个类继承TextView,即自定义view:
加入它的三个构造函数,同时将isFocused()函数的返回值设为true;
package com.example.mooc;
import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewDebug.ExportedProperty;
import android.widget.TextView;
public class MarqueeTextView extends TextView {
public MarqueeTextView(Context context) {
super(context);
}
public MarqueeTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MarqueeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
@ExportedProperty(category = "focus")
public boolean isFocused() {
return true;
}
}
2.使用自定义的TextView:
<com.example.mooc.MarqueeTextView
android:layout_width="wrap_content"
最后两个就一起滚动了:
5.总结:
这种实现只会循环一次,即看完整个字符串就不会再动了,并不会无限循环。