一、如何在Android中顯示長文本?java
在Android中,當咱們要顯示長文本的時候,若是不作任何操做,僅僅是在TextView中顯示,則會自動換行。android
<!--string.xml--> <string name="app_name">MyForthAndroid</string> <string name="action_settings">Settings</string> <string name="hello_world">認真看完這段話的人是豬!認真看完這段話的人是豬! 認真看完這段話的人是豬!</string> //===================================================================== <!--acticity_main.xml--> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" />
二、不少時候咱們佈局要求長文本只能用一行顯示,咱們不但願他換行,則能夠對acticity_main.xml文件作以下修改:app
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:text="@string/hello_world" />
三、咱們能夠看到是能夠實現只在一行顯示了,但是文本顯示不徹底並且後面還有個很難看的省略號,這個該怎麼解決呢?看來acticity_main.xml還須要作必定修改函數
<TextView 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" />
四、咱們成功地實現了跑馬燈效果,可是新的問題又來了。每每在一個項目中,咱們須要實現跑馬燈效果的單行長文本不少,若是咱們要實現多行長文本的跑馬燈效果,是否是僅僅只是簡單的把第一個TextView複製就行了?咱們來試試效果。咱們在acticity_main.xml文件中做以下修改。佈局
<TextView android:id="@+id/textview1" 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" /> <TextView android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:singleLine="true" android:text="@string/hello_world" />
五、咱們看到,事實上只有第一行成功實現了跑馬燈的效果,第二行並無實現這個效果。那咱們應該怎麼作呢?code
咱們建立一個MarqueeText的類,繼承自TextView。咱們自動生成它的全部構造方法,而後重載它的一個內部方法isFocused()。而後將以前全部的TextView所有轉爲MarqueeText,代碼以下:xml
<!--MarqueeText.java--> public class MarqueeText extends TextView{ public MarqueeText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } public MarqueeText(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } public MarqueeText(Context context) { super(context); // TODO Auto-generated constructor stub } public boolean isFocused(){ return true; } } //====================================================================== <!--activity_main.xml> <com.example.myforthandroid.MarqueeText android:id="@+id/textview1" 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.myforthandroid.MarqueeText android:layout_below="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:singleLine="true" android:text="@string/hello_world" />
注意:繼承
一、一鍵生成全部構造函數的方法:Source-Generate Constructors From SuperClass,它默認會幫咱們把全部的構造函數勾選,咱們點擊肯定便可。ip
二、爲何單純的複製不能實現跑馬燈的效果,而必須實現isFocused方法才能夠?ci
由於底層默認把觸摸點也就是關注點給了TextView1,這樣TextView2天然沒法獲取關注,這樣天然就不能實現效果。咱們新建一個類,實現了isFocused方法,將全部的TextView都設置爲關注點,因此就能夠實現這樣的效果了。
isFocused方法的做用是,確認當前這個對想是否處於被選中的狀態,咱們在判斷的時候,都return true,也就是說,當前這兩個textView都處於被選中的狀態。
三、通常咱們在開發中設置距離有三種單位:px(像素值),dip(sp),dp
通常不建議用px,由於他不能根據分辨率來進行縮放,他只能是多少就是多少。
建議使用dip和sp,他們可以根據分辨率來調整自身大小。可是sp更多的是用在文字的顯示上。
那麼dp和dip咱們應該用哪一個呢?事實上在安卓最新的sdk中,已經慢慢推薦你們使用dp,以dp爲單位。