如何在 Android 中使用 ScrollBar?


此示例演示如何在 android 中使用 ScrollView。

步驟 1 − 在 Android Studio 中建立一個新專案,轉到 File ⇒ New Project 並填寫所有必需的詳細資訊以建立一個新專案。

步驟 2 − 新增以下程式碼到 res/layout/activity_main.xml。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:padding="16sp"
   android:scrollbarSize="25sp"
   tools:context=".MainActivity">
   <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      android:padding="8dp"
      android:gravity="center_horizontal">
      <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Button" />
      <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Button" />
      <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Button" />
      <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Button" />
      <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Button" />
      <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Button" />
      <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Button" />
      <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Button" />
      <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Button" />
      <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Button" />
      <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Button" />
      <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Button" />
      <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Button" />
      <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Button" />
      <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Button" />
      <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Button" />
      <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Button" />
      <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Button" />
      <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Button" />
      <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Button" />
      <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Button" />
   </LinearLayout>
</ScrollView>

步驟 3 − 新增以下程式碼到 src/MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
   }
}

步驟 4 − 新增以下程式碼到 androidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.com.sample">
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      <activity android:name=".MainActivity">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

讓我們嘗試執行你的應用程式。我假設你已經將你的安卓手機與你的電腦連線起來了。要從安卓工作室執行該應用程式,開啟一個專案的活動檔案並點選執行 播放圖示 工具欄中的圖示。選擇你的移動裝置作為選項,然後檢查你的移動裝置,它將顯示你的預設螢幕 -

點選 這裡 下載專案程式碼。

更新於: 2020 年 7 月 3 日

4K+ 檢視

開啟你的職業生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.