Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Cách bật thanh cuộn dọc cho WebView Android

Giới thiệu

Hướng dẫn này trình bày cách bật thanh cuộn dọc (vertical scrollbar) cho WebView Android, cùng với việc thiết lập encoding văn bản mặc định.

Các bước thực hiện

Bước 1: Tạo dự án mới

Tạo dự án Android Studio mới: File → New Project.

Bước 2: Layout activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <WebView
        android:id="@+id/web_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

Bước 3: MainActivity.java

package com.example.myapplication;

import android.os.Build;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.app.ProgressDialog;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Đang tải dữ liệu...");
        progressDialog.setCancelable(false);

        WebView webView = findViewById(R.id.web_view);
        
        // BẬT THANH CUỘN DỌC
        webView.setVerticalScrollBarEnabled(true);
        
        webView.requestFocus();
        
        WebSettings webSettings = webView.getSettings();
        webSettings.setDefaultTextEncodingName("utf-8");
        webSettings.setJavaScriptEnabled(true);
        
        webView.loadUrl("https://touch.facebook.com/");

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });

        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress < 100) {
                    progressDialog.show();
                }
                if (newProgress == 100) {
                    progressDialog.dismiss();
                }
            }
        });
    }
}

Bước 4: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.myapplication">

    <uses-permission android:name="android.permission.INTERNET"/>

    <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>
                

Các phương thức liên quan đến Scrollbar

Phương thứcMô tả
setVerticalScrollBarEnabled(true)Bật thanh cuộn dọc (mặc định: true)
setHorizontalScrollBarEnabled(true)Bật thanh cuộn ngang
setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY)Thanh cuộn đè lên nội dung
setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY)Thanh cuộn ngoài viền (mặc định)
setVerticalScrollbarPosition(View.SCROLLBAR_POSITION_LEFT/RIGHT)Vị trí thanh cuộn dọc

Lưu ý về setDefaultTextEncodingName

Phương thức setDefaultTextEncodingName("utf-8") đảm bảo WebView hiển thị đúng tiếng Việt và các ký tự Unicode khi server không gửi header charset chính xác. Tuy nhiên, ưu tiên nên cấu hình server trả về header Content-Type: text/html; charset=utf-8.