android 之textview滾動,,跑馬燈

一定很羨慕app中廣告的動態效果,小白這裏自學了這種效果。
很簡單 只需要簡單的設置幾個屬性就ok了。

上代碼,求建議。請看代碼

1、這裏是佈局文件:

<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"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >

 

    <TextView

        android:id="@+id/textview"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

         android:background="#00ffff"

        android:textColor="#ff00ff"

        android:singleLine="true"

        android:ellipsize="marquee"

        android:marqueeRepeatLimit="marquee_forever"

        android:focusable="true"

        android:focusableInTouchMode="true"

       />

 

</RelativeLayout>

2、這裏是activity源碼

package com.example.gundongtextview;

 

import android.os.Bundle;

import android.app.Activity;

import android.text.Html;

import android.text.method.LinkMovementMethod;

import android.view.Menu;

import android.widget.TextView;

 

public class MainActivity extends Activity {

private TextView tv;

 

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

tv=(TextView) findViewById(R.id.textview);

String str="16日到24日進行的國考報名已進入“下半場”,截至昨天17:00,已經有將近39萬名考生通過審覈,仍有703個職位無人報考。";

tv.setText(str);

//當然可以用html來設置textview內容,,添加超鏈接等,此處不贅述tv.setMovementMethod(LinkMovementMethod.getInstance());//可以不寫這句。最好加上。

}

}

 

3、個人感受。屬性很重要,然後就是可以在文本中做其他操作,比如弄個超鏈接--個性嘛。隨便啦。

  第一點,首先確保單行文本,即singleLine

第二點,設置markquee  並且設置重複模式

第三點,能夠獲得焦點

第四點:

android:focusableInTouchMode="true"

這個屬性很重要,不能少。

 

 

 

歡迎技術交流。QQ964079433

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章