Android學習之Textview的一些屬性解釋

來源:互聯網
上載者:User

標籤:android   color   os   ar   for   sp   art   on   c   

delia

動態改變控制項的方法


1.聲明控制項參數擷取的對象

LinearLayout.LayoutParams linear = (LayoutParams) view.getLayoutParams();

2.設定控制項參數,如寬度:

linear.width = 10;

3.使得設定生效

view.setLayoutParams(linear);

注意:view表示的是對應的控制項對象

posted @ 2012-03-01 11:29 delia 閱讀(5) 評論(0) 編輯

讓TextView 內建捲軸


TextView中有個ellipsize屬性,作用是當文字過長時,該控制項該如何顯示,解釋如下:

1.android:ellipsize=”start”—–省略符號顯示在開頭

2.android:ellipsize=”end”——省略符號顯示在結尾

3.android:ellipsize=”middle”—-省略符號顯示在中間

4.android:ellipsize=”marquee”–以跑馬燈的方式顯示(動畫橫向移動)

文字左右滾動三個屬性:

android:singleLine=”true”

android:ellipsize=”marquee”

android:marqueeRepeatLimit=”marquee_forever”

Android中我們為了實現文本的滾動可以在ScrollView中嵌入一個TextView,其實TextView自己也可以實現多行滾動的,畢竟 ScrollView必須只能有一個直接的子類布局。只要在layout中簡單設定幾個屬性就可以輕鬆實現。

<TextView

android:id=”@+id/tvCWJ”

android:layout_width=”fill_parent”

android:layout_height=”wrap_content”

android:scrollbars=”vertical” <!–垂直捲軸 –>

android:singleLine=”false” <!–實現多行 –>

android:maxLines=”15″ <!–最多不超過15行 –>

android:textColor=”#FF0000″

/>

< TextView

android:id = ”@+id/app_shortcontent”

android:layout_width = ”wrap_content”

android:layout_height = ”wrap_content”

android:singleLine = ”true”

android:textColor = ”#FFFFFFFF”

android:scrollHorizontally = ”true”

android:focusable = ”true”

android:ellipsize = ”marquee”

android:marqueeRepeatLimit = ”marquee_forever” />

當然我們為了讓TextView動起來,還需要用到TextView的setMovementMethod方法設定一個滾動執行個體,代碼如下

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

tv.setMovementMethod(ScrollingMovementMethod.getInstance()); // Android開發網提示相關的可以查看SDK中android.text.method分支瞭解更多

附:

順便講下 TextView 自動滾動的實現方法,下面介紹兩種方法:

一、在代碼中實現:

textView .setEllipsize(TextUtils.TruncateAt.MARQUEE);

textView .setSingleLine(true);

textView .setMarqueeRepeatLimit(6);

二、在XML中實現:

<TextView android:id=”@+id/TextView01″ android:layout_width=”wrap_content”

android:layout_height=”wrap_content” android:singleLine=”true”

android:text=”dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd”

android:marqueeRepeatLimit=”marquee_forever” android:ellipsize=”marquee”

android:scrollHorizontally=”true” android:width=”150dip”></TextView>

一切OK,當 textView 擷取焦點後,就會自動滾動。

補充 ==============================

不管是手動還是自動 你要實現滾動就得加scrollview

到時候可以通過handle來調用scrollview的scrollTo方法實現滾動

Handler mHandler = new Handler();

private Runnable mScrollToBottom = new Runnable() {

public void run() {

mScrollView.scrollTo(0, offset);

}

};

onTouch裡面

mHandler.post(mScrollToBottom);

補充 ====================

補充:

1,把 textview的 Ellipsize 設定成 marquee(上面有說)

2,把 Deprecated的 Single line 設定成 true

3,設定textview的Marquee repeat limit 屬性(滾動回數,預設是無數回)

貼個代碼吧.

textview.setEllipsize(TextUtils.TruncateAt.MARQUEE);

textview.setSingleLine(true);

textview.setMarqueeRepeatLimit(6);

Android學習之Textview的一些屬性解釋

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.