需求:
当一行文字过长,屏幕显示不了全部文字(单行显示情况下),可以用TextView的跑马灯效果来显示文字。
第一步
在layout的activity_mail.xml中定义两个TextView如下:
<TextView
android:id="@+id/textview1" //定义id
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true" //单行显示
android:ellipsize="marquee" //省略方式:跑马灯
android:text="@string/hello_world" //要在string中重新定义hello_world
android:focusable="true"
android:focusableInTouchMode="true"
/>
<TextView
android:layout_below="@id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" //与textView1有间隔
android:singleLine="true"
android:ellipsize="marquee"
android:text="@string/hello_world"
android:focusable="true"
android:focusableInTouchMode="true"
/>
第二步:
如果不定义一个类,单纯在xml设置属性,则只有第一个TextView有跑马灯效果
定义一个类,继承TextView,并实现三个构造方法和一个普通方法
(补全子类函数技巧:鼠标先指向MarqueeTest,增加一个构造函数,然后再鼠标右键-->Source-->Generate Constructors from Superclass)
package com.imooc.marqueetextviewdemo;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
public class MarqueeTest extends TextView{
public MarqueeTest(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MarqueeTest(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public MarqueeTest(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
public boolean isFocused() {
// TODO Auto-generated method stub
return true;
}
}
第三步:
将layout中的TextView改成MarqueeTest类
做法:<TextView ---> <com.imooc.marqueetextviewdemo.MarqueeTest //两条都要改
PS: 在使用单位符号时有三种
ps是多少就是多少
dp(更多使用),dip 在控件中做长度,大小的使用时
sp更多地推介在显示文字上-
当一行文字过长,屏幕显示不了全部文字(单行显示情况下),可以用TextView的跑马灯效果来显示文字。
第一步
在layout的activity_mail.xml中定义两个TextView如下:
<TextView
android:id="@+id/textview1" //定义id
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true" //单行显示
android:ellipsize="marquee" //省略方式:跑马灯
android:text="@string/hello_world" //要在string中重新定义hello_world
android:focusable="true"
android:focusableInTouchMode="true"
/>
<TextView
android:layout_below="@id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" //与textView1有间隔
android:singleLine="true"
android:ellipsize="marquee"
android:text="@string/hello_world"
android:focusable="true"
android:focusableInTouchMode="true"
/>
第二步:
如果不定义一个类,单纯在xml设置属性,则只有第一个TextView有跑马灯效果
定义一个类,继承TextView,并实现三个构造方法和一个普通方法
(补全子类函数技巧:鼠标先指向MarqueeTest,增加一个构造函数,然后再鼠标右键-->Source-->Generate Constructors from Superclass)
package com.imooc.marqueetextviewdemo;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
public class MarqueeTest extends TextView{
public MarqueeTest(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MarqueeTest(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public MarqueeTest(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
public boolean isFocused() {
// TODO Auto-generated method stub
return true;
}
}
第三步:
将layout中的TextView改成MarqueeTest类
做法:<TextView ---> <com.imooc.marqueetextviewdemo.MarqueeTest //两条都要改
PS: 在使用单位符号时有三种
ps是多少就是多少
dp(更多使用),dip 在控件中做长度,大小的使用时
sp更多地推介在显示文字上-