在layout里面添加TextView控件
正常的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
首先让这个类继承TextView,之后添加`
public other(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public other(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO Auto-generated constructor stub
}
public other(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
public boolean isFocused() {
// TODO Auto-generated method stub
return true;
}`
- 使用自定义的TextView
还是之前的那个TextView我又复制了一个,把系统的TextView换成了自定义的TextView
引用自定义的TextView是全包名加类名
<com.example.texttest.other
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"
/>
<com.example.texttest.other
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"
/>
这样的话多行滚动就已经实现了,新人第一次写有不足的地方请多多指教,一边学习,一边把学习的成果发布出来