如何以程式設計方式在 Android 中停用 ScrollView?


本示例演示瞭如何以程式設計方式在 Android 中停用 ScrollView。

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

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

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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="16dp"
   tools:context=".MainActivity">
   <ScrollView
      android:id="@+id/scrollView"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
      <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:gravity="center"
         android:orientation="vertical"
         android:padding="16dp">
         <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" />
         <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>
</RelativeLayout>

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

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Build;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
   @RequiresApi(api = Build.VERSION_CODES.M)
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      ScrollView scrollView = findViewById(R.id.scrollView);
      scrollView.setOnTouchListener(new View.OnTouchListener() {
         @Override
         public boolean onTouch(View v, MotionEvent event) {
            Toast.makeText(MainActivity.this, "ScrollView Disabled", Toast.LENGTH_SHORT).show();
            return true;
         }
      });
   }
}

步驟 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" />
            <ategory android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

嘗試執行你的應用程式。我假設你已將你的真實 Android 移動裝置與電腦相連。要從 Android Studio 執行應用,開啟你的一個專案活動檔案,然後單擊工具欄上的執行  圖示。選擇你的移動裝置作為選項,然後檢視你的移動裝置,它將顯示你的預設螢幕 −

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

更新日期: 2019 年 11 月 22 日

3k+ 瀏覽

啟動您的 職業

透過完成課程獲得認證

立即開始
廣告