用户界面View之TextView(实现跑马灯效果)

来源:互联网 发布:淘宝零点抢购在哪里 编辑:程序博客网 时间:2024/06/16 20:31
向前吧,荡起生命之舟,不必依恋和信泊,破浪的船自会一路开放常新的花朵。 

本讲内容:TextView 文本
并认识一下使用setMovementMethod()方法实现文本可滚动

示例一:


下面是布局文件:
<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.text.MainActivity$PlaceholderFragment" >     <TextView         android:id="@+id/textViewId"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world"        android:textSize="30sp"/></RelativeLayout>

下面是主文件:
package com.example.text;import android.app.Activity;import android.os.Bundle;import android.text.method.ScrollingMovementMethod;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class MainActivity extends Activity {private TextView t1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);t1=(TextView) findViewById(R.id.textViewId);//设置移动方法  使用setMovementMethod()方法实现文本可滚动t1.setMovementMethod(ScrollingMovementMethod.getInstance());}} 

下面是string.xml文件:
<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">Text</string>    <string name="hello_world">曾经有一年春天, 我走过一棵树下,看着那树上的某一片叶子,我很喜欢。         等秋天时,在那秋风起中,我又来到这棵树下,但却找不到那我喜欢的绿叶,我以为,我找不到它了。         可是我却不知道,它还在那里,只是颜色改变,等我带着惆怅转身离开,秋风中那树上的一片枯叶被卷起,         在我身边环绕,伴随我一路,但我惆怅中还在思恋那曾经的绿叶,却不知,原来我要等的,要找的,         在我的身边.........</string>    <string name="action_settings">Settings</string></resources>

示例二:实现跑马灯效果



下面是res/layout/activity_main.xml 布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@drawable/bg" >    <TextView         android:layout_width="match_parent"        android:layout_height="wrap_content"        android:focusable="true"         android:singleLine="true"        android:ellipsize="marquee"        android:marqueeRepeatLimit="marquee_forever"        android:focusableInTouchMode="true"        android:scrollHorizontally="true"        android:textSize="25sp"        android:text="@string/hello_world"/></LinearLayout>

<!--激活焦点-->   android:focusable="true"    <!--单行显示-->  android:singleLine="true"  <!--这里设置为超出文本后滚动显示-->  android:ellipsize="marquee"  <!--这个是设置滚动几次,这里是无限循环-->  android:marqueeRepeatLimit="marquee_forever"         <!--TouchMode模式的焦点激活-->  android:focusableInTouchMode="true"   <!--横向超出后是否有横向滚动条-->  android:scrollHorizontally="true"  

示例三:如果有多个TextView要实现该效果,该方法显示不能满足,因为被第一个TextView抢得焦点,其余不能
public class MarqueeText extends TextView{public MarqueeText(Context context) {super(context);}public MarqueeText(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}public MarqueeText(Context context, AttributeSet attrs) {super(context, attrs);}public boolean isFocused() {return true;//使每个Tv都激活焦点}}




下面是res/layout/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" >    <com.example.tvtest.MarqueeText        android:id="@+id/id_tv1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:ellipsize="marquee"        android:focusable="true"        android:focusableInTouchMode="true"        android:singleLine="true"        android:text="@string/hello_world" />    <com.example.tvtest.MarqueeText        android:id="@+id/id_tv2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/id_tv1"        android:ellipsize="marquee"        android:focusable="true"        android:layout_marginTop="10dp"        android:focusableInTouchMode="true"        android:singleLine="true"        android:text="@string/hello_world" /></RelativeLayout>



Take some time and enjoy it

0 0