您的位置:首页 > 移动开发 > Android开发

TextView实现跑马灯效果

2014-11-05 22:33 281 查看
使用TextView简单实现灯箱跑马灯效果:

直接上代码:activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.t.MainActivity" >

<TextView
android:id="@+id/notice"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:ellipsize="marquee"//以横向滚动方式显示(需获得当前焦点时)
android:focusable="true"//允许获取焦点
android:focusableInTouchMode="true"//设置是否触摸模式。
android:gravity="center"
android:marqueeRepeatLimit="marquee_forever"//一直滚动
android:scrollHorizontally="true"//设置文本超出TextView长度的情况下,是否出现横拉条
android:singleLine="true"//单行显示
android:textSize="20sp"
android:textColor="#FF0000"
android:text="回收二手航母、导弹、潜艇、坦克、翻新原子弹啦!!!!..."
android:textStyle="bold" />

</RelativeLayout>


JAVA代码:MainActivity.java

package com.example.t;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);//无标题
setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
效果图:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android textview 跑马灯