转载自: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控件都在滚动: