1. 写一个类继承TextView
代码如下 | 复制代码 |
packagecom.example.tt; importandroid.content.Context; importandroid.graphics.Rect; importandroid.util.AttributeSet; importandroid.widget.TextView; publicclassScrollingTextViewextendsTextView { publicScrollingTextView(Context context, AttributeSet attrs,intdefStyle) { super(context, attrs, defStyle); } publicScrollingTextView(Context context, AttributeSet attrs) { super(context, attrs); } publicScrollingTextView(Context context) { super(context); } @Override protectedvoidonFocusChanged(booleanfocused,intdirection, Rect previouslyFocusedRect) { if(focused) super.onFocusChanged(focused, direction, previouslyFocusedRect); } @Override publicvoidonWindowFocusChanged(booleanfocused) { if(focused) super.onWindowFocusChanged(focused); } @Override publicbooleanisFocused() { returntrue; } } |
2. xml 中增加属性
代码如下 | 复制代码 |
android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="80dip" android:layout_marginBottom="25dip" android:textSize="25sp" android:singleLine="true" android:textColor="@android:color/black" android:ellipsize="marquee" android:focusable="true" android:marqueeRepeatLimit="marquee_forever" android:focusableInTouchMode="true" android:scrollHorizontally="true" android:text="这才是真正的文字跑马灯效果,文字移动速度,文字移动方向,文字移动的样式,动画等等……" android:background="#2FFFFFFF"/> |
3. 在activity中声明
代码如下 | 复制代码 |
packagecom.example.tt; importandroid.os.Bundle; importandroid.app.Activity; importandroid.view.Menu; publicclassMainActivityextendsActivity { @Override publicvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } } |