多个TextView跑马灯效果

转载自:http://blog.csdn.net/muyu114/article/details/6400563

生成新类AlwaysMarqueeTextView:

package com.tutor.servicedemo;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * Created by IntelliJ IDEA.
 * User: wulong
 * Date: 11-12-5
 * Time: 上午8:13
 * To change this template use File | Settings | File Templates.
 */
public class AlwaysMarqueeTextView extends TextView {

    public AlwaysMarqueeTextView(Context context) {
        super(context);
    }

    public AlwaysMarqueeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AlwaysMarqueeTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean isFocused() {
        return true;
    }
}

main.xml配置文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:addStatesFromChildren="true"
        >
    <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:text="hello what can i dor for you ,do you know what is an interest thing , i think you do not know"
            android:focusable="true"
            android:ellipsize="marquee"
            android:marqueeRepeatLimit="marquee_forever"
            android:focusableInTouchMode="true"
            />

    <com.tutor.servicedemo.AlwaysMarqueeTextView
            android:id="@+id/second"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:text="hello what can i dor for you ,do you know what is an interest thing , i think you do not know"
            android:focusable="true"
            android:ellipsize="marquee"
            android:marqueeRepeatLimit="marquee_forever"
            android:focusableInTouchMode="true"
            />

    <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="hewiofjew"
            />
</LinearLayout>

JAVA文件:

package com.tutor.servicedemo;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import static android.view.KeyEvent.KEYCODE_BACK;

public class MyTest extends Activity {

    private static final String TAG = "TestDemo.MyTest";
    private MyService mMyService;
    private TextView mTextView;
    private Button startServiceButton;
    private Button stopServiceButton;
    private Button bindServiceButton;
    private Button unbindServiceButton;
    private Context mContext;

    //这里需要用到ServiceConnection在Context.bindService和context.unBindService()里用到
    private ServiceConnection mServiceConnection = new ServiceConnection() {

        //当我bindService时,让TextView显示MyService里getSystemTime()方法的返回值
        public void onServiceConnected(ComponentName name, IBinder service) {
            // TODO Auto-generated method stub
            mMyService = ((MyService.MyBinder) service).getService();
            mTextView.setText("I am frome Service :" + mMyService.getSystemTime());
            Log.e(TAG, "onServiceConnected");
        }

        public void onServiceDisconnected(ComponentName name) {
            // TODO Auto-generated method stub
            Log.e(TAG, "onServiceDisconnected");

        }
    };

    public void onCreate(Bundle savedInstanceState) {
        Log.e(TAG, "onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView text = (TextView) findViewById(R.id.text);

//        text.setTextSize(30);
//        text.setFocusable(true);
    }


    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KEYCODE_BACK:
                finish();
                return true;
            default:
                break;
        }
        return super.onKeyDown(keyCode, event);
    }


}
效果,两个TextView控件都在滚动:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值