iT邦幫忙

2021 iThome 鐵人賽

DAY 5
0
Mobile Development

android studio 30天初學筆記系列 第 5

Android Studio初學筆記-Day5-TextView

基本元件介紹-TextView

接下來會開始介紹android studio一些基本的元件,這些元件雖然看似簡單不過也有不少屬性可以設定,這些元件也將在未來不管大大小小的專案中不斷出現,所以盡可能練習用到得心應手,對未來寫程式也會順利不少。
首先介紹幾個基本的屬性設定:

  • android:text="";
    負責設定顯示的文字。
  • android:textSize="";
    負責設定文字大小,使用的單位是sp。
    在textSize的設定上對單位有特別的限制,若單位不是用sp而是用dp,雖然在畫面上不會有甚麼差異,不過使用dp設定的話會產生字體不會隨著設定放大縮小,如老花模式等,所以文字的大小建議養成使用sp的習慣。
  • android:textStyle="bold"
    文字粗體。
  • android:textStyle="italic"
    文字斜體。
  • android:textColor="";
    負責設定文字的顏色。
  • android:background="";
    負責設定背景顏色。
    顏色的設定可以選擇輸入色碼,網路上也有許多色碼表可以參考。或著是打開res/value/color.xml 的檔案先對顏色設定,好方便之後直接使用。像是如下方的設定:
<resources>
    <color name="purple_200">#FFBB86FC</color>
    <color name="purple_500">#FF6200EE</color>
    <color name="purple_700">#FF3700B3</color>
    <color name="teal_200">#FF03DAC5</color>
    <color name="teal_700">#FF018786</color>
    <color name="black">#FF000000</color>
    <color name="white">#FFFFFFFF</color>
    <color name="red">#FF0000</color>
    <color name="green">#00FF00</color>
    <color name="yellow">#FFFF00</color>
</resources>

以下為簡單的例子

程式碼

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tx1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="大小20sp"
        android:textStyle="italic"
        android:textColor="#009688"
        android:textSize="20sp"/>
    <TextView
        android:id="@+id/tx2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="大小40sp"
        android:textStyle="bold"
        android:textColor="@color/green"
        android:textSize="40sp"/>
    <TextView
        android:id="@+id/tx3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="背景黑色"
        android:background="@color/black"
        android:textColor="@color/yellow"
        android:textSize="30sp"/>
</LinearLayout>

https://ithelp.ithome.com.tw/upload/images/20210907/201391368HxYwu4ac1.png
TextView就簡單介紹到這裡,謝謝大家/images/emoticon/emoticon41.gif


上一篇
Android Studio初學筆記-Day4-ConstaintLayout
下一篇
Android Studio初學筆記-Day6-EditText
系列文
android studio 30天初學筆記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言