Android開發——控制元件TextView的用法(常用屬性)
阿新 • • 發佈:2019-02-03
我們新建的Android Application專案一般會有幫我們寫好"Hello World!"顯示在我們的小手機中,這個"Hello World!"就是TextView控制元件寫出來的,下面我們就簡單地介紹一下"TextView"的用法,談一談它的基本屬性。
1.android:id 唯一地標識控制元件的ID;<TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FF0000" android:textSize="15pt" android:text="@string/hello_world" android:singleLine="true"></TextView>
2.android:layout_width 控制元件的寬度有三個值wrap_content、fill_parent、match_parent,這裡面fill_parent和match_parent是一樣的效果,都是填滿父容器,現在wrap_content是根據包裹的內容自適應控制元件大小,但是不能超過父容器寬度。當然我們可以自己手動設定寬度(單位dp、sp、pt、mm、in、px);
3.android:layout_height 控制元件的高度,值和android:layout_width相同;
4.android:textColor 文字字型的顏色;
5.android:textSize 文字字型的大小;
6.android:text 文字的內容上面的程式碼"@string/hello_world"是引用了res/values下的"string.xml"檔案,找到名字為"hello_world"的string,裡面的值就是文字的值。我們也可以直接把文字的值寫在雙引號中;
7.android:singleLine singleLine屬性表示是否文字只顯示一行,預設為false。如果為true,用...來代替超出部分。
好了,TextView就說這些,其他的以後慢慢接觸。