iT邦幫忙

2021 iThome 鐵人賽

DAY 13
0
Mobile Development

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

Android Studio初學筆記-Day13-ScrollView

ScrollView

今天要介紹的元件,當介面的內容開始變多時就派上用場了,畢竟手機螢幕或著各類3c顯示的大小都相當有限,在有大量內容或者製作類似電子書的樣式時,可以透過ScrollView來製造滑動螢幕的效果,彌補螢幕不足的問題,使用方式也很簡單,把需要滑動的內容放置ScrollView的標籤之中即可,簡單的示範配置如下。

程式碼

<androidx.constraintlayout.widget.ConstraintLayout 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">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout_editor_absoluteX="159dp"
        tools:layout_editor_absoluteY="99dp"
        android:scrollbarStyle="insideInset">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TextView介紹"
                android:textSize="30sp"
                android:textStyle="bold" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/textview"
                android:textSize="20sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/EditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="EditView"
                android:textSize="30sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/textView3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/edittext"
                android:textSize="20sp" />
        </LinearLayout>
    </ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

這裡介紹一下ScrollView的屬性
android:scrollbarStyle 此屬性是設定滑動的樣式,包括以下四種:

  1. insideOverlay(預設值)
  2. insideInset
  3. outsideOverlay
  4. outsideInset
    各位可以依序設定看看不一樣的效果,或者不想要有滑動條的顯示則可以設定android:scrollbars="none"。
    效果如下:
    https://ithelp.ithome.com.tw/upload/images/20210907/20139136oWFf3B1EEi.png
    這裡要補充一下,ScrollView還有一個兄弟名叫HorizontalScrollView,從名字可以想像的到它的功能就是橫向的ScrollView,而在用法上也可以說是相同的,所以這裡就不再多做呈現,有興趣的朋友可以當作熟悉ScrollView的用法來再玩一遍嘗試看看喔。
    今天的ScrollView就講到這裡了,謝謝大家!/images/emoticon/emoticon41.gif

上一篇
Android Studio初學筆記-Day12-Spinner
下一篇
Android Studio初學筆記-Day14-Switch和Toggle Button
系列文
android studio 30天初學筆記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言