Tạo tệp html trong studio android

In ra nội dung ngoài một bức ảnh đơn giản trên Android yêu cầu phải soạn văn bản và đồ họa trong tài liệu in. Khung Android cung cấp cách sử dụng HTML để soạn tài liệu và in tài liệu đó với ít mã nhất

Trong Android4. 4 (API cấp 19), lớp

private WebView mWebView;

private void doWebViewPrint() {
    // Create a WebView object specifically for printing
    WebView webView = new WebView(getActivity());
    webView.setWebViewClient(new WebViewClient() {

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                Log.i(TAG, "page finished loading " + url);
                createWebPrintJob(view);
                mWebView = null;
            }
    });

    // Generate an HTML document on the fly:
    String htmlDocument = "

Test Content

Testing, " + "testing, testing...

"; webView.loadDataWithBaseURL(null, htmlDocument, "text/HTML", "UTF-8", null); // Keep a reference to WebView object until you pass the PrintDocumentAdapter // to the PrintManager mWebView = webView; }
8 đã được cập nhật để cho phép in nội dung HTML. Lớp này cho phép bạn tải tài nguyên HTML cục bộ hoặc tải xuống một trang từ web, tạo lệnh in và gửi nó cho các dịch vụ in của Android

Bài học này chỉ cho bạn cách tạo nhanh một tài liệu HTML có chứa văn bản và đồ họa và sử dụng

private WebView mWebView;

private void doWebViewPrint() {
    // Create a WebView object specifically for printing
    WebView webView = new WebView(getActivity());
    webView.setWebViewClient(new WebViewClient() {

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                Log.i(TAG, "page finished loading " + url);
                createWebPrintJob(view);
                mWebView = null;
            }
    });

    // Generate an HTML document on the fly:
    String htmlDocument = "

Test Content

Testing, " + "testing, testing...

"; webView.loadDataWithBaseURL(null, htmlDocument, "text/HTML", "UTF-8", null); // Keep a reference to WebView object until you pass the PrintDocumentAdapter // to the PrintManager mWebView = webView; }
8 để in tài liệu đó

Tải một tài liệu HTML

In tài liệu HTML bằng

private WebView mWebView;

private void doWebViewPrint() {
    // Create a WebView object specifically for printing
    WebView webView = new WebView(getActivity());
    webView.setWebViewClient(new WebViewClient() {

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                Log.i(TAG, "page finished loading " + url);
                createWebPrintJob(view);
                mWebView = null;
            }
    });

    // Generate an HTML document on the fly:
    String htmlDocument = "

Test Content

Testing, " + "testing, testing...

"; webView.loadDataWithBaseURL(null, htmlDocument, "text/HTML", "UTF-8", null); // Keep a reference to WebView object until you pass the PrintDocumentAdapter // to the PrintManager mWebView = webView; }
8 liên quan đến việc tải tài nguyên HTML hoặc xây dựng tài liệu HTML dưới dạng chuỗi. Phần này mô tả cách xây dựng một chuỗi HTML và tải nó vào một
private WebView mWebView;

private void doWebViewPrint() {
    // Create a WebView object specifically for printing
    WebView webView = new WebView(getActivity());
    webView.setWebViewClient(new WebViewClient() {

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                Log.i(TAG, "page finished loading " + url);
                createWebPrintJob(view);
                mWebView = null;
            }
    });

    // Generate an HTML document on the fly:
    String htmlDocument = "

Test Content

Testing, " + "testing, testing...

"; webView.loadDataWithBaseURL(null, htmlDocument, "text/HTML", "UTF-8", null); // Keep a reference to WebView object until you pass the PrintDocumentAdapter // to the PrintManager mWebView = webView; }
8 để in

Đối tượng dạng xem này thường được sử dụng như một phần của bố cục hoạt động. Tuy nhiên, nếu ứng dụng của bạn không sử dụng

private WebView mWebView;

private void doWebViewPrint() {
    // Create a WebView object specifically for printing
    WebView webView = new WebView(getActivity());
    webView.setWebViewClient(new WebViewClient() {

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                Log.i(TAG, "page finished loading " + url);
                createWebPrintJob(view);
                mWebView = null;
            }
    });

    // Generate an HTML document on the fly:
    String htmlDocument = "

Test Content

Testing, " + "testing, testing...

"; webView.loadDataWithBaseURL(null, htmlDocument, "text/HTML", "UTF-8", null); // Keep a reference to WebView object until you pass the PrintDocumentAdapter // to the PrintManager mWebView = webView; }
8, bạn có thể tạo một thể hiện của lớp cụ thể cho mục đích in ấn. Các bước chính để tạo chế độ xem in tùy chỉnh này là

  1. Tạo một
    private WebView mWebView;
    
    private void doWebViewPrint() {
        // Create a WebView object specifically for printing
        WebView webView = new WebView(getActivity());
        webView.setWebViewClient(new WebViewClient() {
    
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    return false;
                }
    
                @Override
                public void onPageFinished(WebView view, String url) {
                    Log.i(TAG, "page finished loading " + url);
                    createWebPrintJob(view);
                    mWebView = null;
                }
        });
    
        // Generate an HTML document on the fly:
        String htmlDocument = "

    Test Content

    Testing, " + "testing, testing...

    "; webView.loadDataWithBaseURL(null, htmlDocument, "text/HTML", "UTF-8", null); // Keep a reference to WebView object until you pass the PrintDocumentAdapter // to the PrintManager mWebView = webView; }
    3 bắt đầu lệnh in sau khi tải tài nguyên HTML
  2. Tải tài nguyên HTML vào đối tượng
    private WebView mWebView;
    
    private void doWebViewPrint() {
        // Create a WebView object specifically for printing
        WebView webView = new WebView(getActivity());
        webView.setWebViewClient(new WebViewClient() {
    
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    return false;
                }
    
                @Override
                public void onPageFinished(WebView view, String url) {
                    Log.i(TAG, "page finished loading " + url);
                    createWebPrintJob(view);
                    mWebView = null;
                }
        });
    
        // Generate an HTML document on the fly:
        String htmlDocument = "

    Test Content

    Testing, " + "testing, testing...

    "; webView.loadDataWithBaseURL(null, htmlDocument, "text/HTML", "UTF-8", null); // Keep a reference to WebView object until you pass the PrintDocumentAdapter // to the PrintManager mWebView = webView; }
    8

Mẫu mã sau minh họa cách tạo một

private WebView mWebView;

private void doWebViewPrint() {
    // Create a WebView object specifically for printing
    WebView webView = new WebView(getActivity());
    webView.setWebViewClient(new WebViewClient() {

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                Log.i(TAG, "page finished loading " + url);
                createWebPrintJob(view);
                mWebView = null;
            }
    });

    // Generate an HTML document on the fly:
    String htmlDocument = "

Test Content

Testing, " + "testing, testing...

"; webView.loadDataWithBaseURL(null, htmlDocument, "text/HTML", "UTF-8", null); // Keep a reference to WebView object until you pass the PrintDocumentAdapter // to the PrintManager mWebView = webView; }
3 đơn giản và tải một tài liệu HTML được tạo một cách nhanh chóng

Kotlin

private var mWebView: WebView? = null

private fun doWebViewPrint() {
    // Create a WebView object specifically for printing
    val webView = WebView(activity)
    webView.webViewClient = object : WebViewClient() {

        override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest) = false

        override fun onPageFinished(view: WebView, url: String) {
            Log.i(TAG, "page finished loading $url")
            createWebPrintJob(view)
            mWebView = null
        }
    }

    // Generate an HTML document on the fly:
    val htmlDocument =
            "

Test Content

Testing, testing, testing...

" webView.loadDataWithBaseURL(null, htmlDocument, "text/HTML", "UTF-8", null) // Keep a reference to WebView object until you pass the PrintDocumentAdapter // to the PrintManager mWebView = webView }

Java

private WebView mWebView;

private void doWebViewPrint() {
    // Create a WebView object specifically for printing
    WebView webView = new WebView(getActivity());
    webView.setWebViewClient(new WebViewClient() {

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                Log.i(TAG, "page finished loading " + url);
                createWebPrintJob(view);
                mWebView = null;
            }
    });

    // Generate an HTML document on the fly:
    String htmlDocument = "

Test Content

Testing, " + "testing, testing...

"; webView.loadDataWithBaseURL(null, htmlDocument, "text/HTML", "UTF-8", null); // Keep a reference to WebView object until you pass the PrintDocumentAdapter // to the PrintManager mWebView = webView; }

Ghi chú. Đảm bảo lệnh gọi tạo lệnh in của bạn diễn ra theo phương thức

private WebView mWebView;

private void doWebViewPrint() {
    // Create a WebView object specifically for printing
    WebView webView = new WebView(getActivity());
    webView.setWebViewClient(new WebViewClient() {

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                Log.i(TAG, "page finished loading " + url);
                createWebPrintJob(view);
                mWebView = null;
            }
    });

    // Generate an HTML document on the fly:
    String htmlDocument = "

Test Content

Testing, " + "testing, testing...

"; webView.loadDataWithBaseURL(null, htmlDocument, "text/HTML", "UTF-8", null); // Keep a reference to WebView object until you pass the PrintDocumentAdapter // to the PrintManager mWebView = webView; }
6 của
private WebView mWebView;

private void doWebViewPrint() {
    // Create a WebView object specifically for printing
    WebView webView = new WebView(getActivity());
    webView.setWebViewClient(new WebViewClient() {

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                Log.i(TAG, "page finished loading " + url);
                createWebPrintJob(view);
                mWebView = null;
            }
    });

    // Generate an HTML document on the fly:
    String htmlDocument = "

Test Content

Testing, " + "testing, testing...

"; webView.loadDataWithBaseURL(null, htmlDocument, "text/HTML", "UTF-8", null); // Keep a reference to WebView object until you pass the PrintDocumentAdapter // to the PrintManager mWebView = webView; }
3 bạn đã tạo trong phần trước. Nếu bạn không đợi cho đến khi quá trình tải trang kết thúc, bản in ra có thể không đầy đủ hoặc trống hoặc có thể bị lỗi hoàn toàn

Ghi chú. Đoạn mã ví dụ trên chứa một thể hiện của đối tượng

private WebView mWebView;

private void doWebViewPrint() {
    // Create a WebView object specifically for printing
    WebView webView = new WebView(getActivity());
    webView.setWebViewClient(new WebViewClient() {

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                Log.i(TAG, "page finished loading " + url);
                createWebPrintJob(view);
                mWebView = null;
            }
    });

    // Generate an HTML document on the fly:
    String htmlDocument = "

Test Content

Testing, " + "testing, testing...

"; webView.loadDataWithBaseURL(null, htmlDocument, "text/HTML", "UTF-8", null); // Keep a reference to WebView object until you pass the PrintDocumentAdapter // to the PrintManager mWebView = webView; }
8 để nó không bị thu gom rác trước khi lệnh in được tạo. Đảm bảo rằng bạn cũng làm như vậy trong quá trình triển khai của riêng mình, nếu không quá trình in có thể không thành công

Nếu bạn muốn đưa đồ họa vào trang, hãy đặt các tệp đồ họa vào thư mục

private WebView mWebView;

private void doWebViewPrint() {
    // Create a WebView object specifically for printing
    WebView webView = new WebView(getActivity());
    webView.setWebViewClient(new WebViewClient() {

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                Log.i(TAG, "page finished loading " + url);
                createWebPrintJob(view);
                mWebView = null;
            }
    });

    // Generate an HTML document on the fly:
    String htmlDocument = "

Test Content

Testing, " + "testing, testing...

"; webView.loadDataWithBaseURL(null, htmlDocument, "text/HTML", "UTF-8", null); // Keep a reference to WebView object until you pass the PrintDocumentAdapter // to the PrintManager mWebView = webView; }
9 của dự án của bạn và chỉ định một URL cơ sở trong tham số đầu tiên của phương thức
webView.loadDataWithBaseURL(
        "file:///android_asset/images/",
        htmlBody,
        "text/HTML",
        "UTF-8",
        null
)
0, như được hiển thị trong ví dụ mã sau

Kotlin

webView.loadDataWithBaseURL(
        "file:///android_asset/images/",
        htmlBody,
        "text/HTML",
        "UTF-8",
        null
)

Java

webView.loadDataWithBaseURL("file:///android_asset/images/", htmlBody,
        "text/HTML", "UTF-8", null);

Bạn cũng có thể tải một trang web để in bằng cách thay thế phương pháp

webView.loadDataWithBaseURL(
        "file:///android_asset/images/",
        htmlBody,
        "text/HTML",
        "UTF-8",
        null
)
0 bằng phương pháp
webView.loadDataWithBaseURL(
        "file:///android_asset/images/",
        htmlBody,
        "text/HTML",
        "UTF-8",
        null
)
2 như hình bên dưới

Kotlin

private WebView mWebView;

private void doWebViewPrint() {
    // Create a WebView object specifically for printing
    WebView webView = new WebView(getActivity());
    webView.setWebViewClient(new WebViewClient() {

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                Log.i(TAG, "page finished loading " + url);
                createWebPrintJob(view);
                mWebView = null;
            }
    });

    // Generate an HTML document on the fly:
    String htmlDocument = "

Test Content

Testing, " + "testing, testing...

"; webView.loadDataWithBaseURL(null, htmlDocument, "text/HTML", "UTF-8", null); // Keep a reference to WebView object until you pass the PrintDocumentAdapter // to the PrintManager mWebView = webView; }
2

Java

private WebView mWebView;

private void doWebViewPrint() {
    // Create a WebView object specifically for printing
    WebView webView = new WebView(getActivity());
    webView.setWebViewClient(new WebViewClient() {

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                Log.i(TAG, "page finished loading " + url);
                createWebPrintJob(view);
                mWebView = null;
            }
    });

    // Generate an HTML document on the fly:
    String htmlDocument = "

Test Content

Testing, " + "testing, testing...

"; webView.loadDataWithBaseURL(null, htmlDocument, "text/HTML", "UTF-8", null); // Keep a reference to WebView object until you pass the PrintDocumentAdapter // to the PrintManager mWebView = webView; }
3

Khi sử dụng

private WebView mWebView;

private void doWebViewPrint() {
    // Create a WebView object specifically for printing
    WebView webView = new WebView(getActivity());
    webView.setWebViewClient(new WebViewClient() {

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                Log.i(TAG, "page finished loading " + url);
                createWebPrintJob(view);
                mWebView = null;
            }
    });

    // Generate an HTML document on the fly:
    String htmlDocument = "

Test Content

Testing, " + "testing, testing...

"; webView.loadDataWithBaseURL(null, htmlDocument, "text/HTML", "UTF-8", null); // Keep a reference to WebView object until you pass the PrintDocumentAdapter // to the PrintManager mWebView = webView; }
8 để tạo tài liệu in, bạn nên lưu ý những hạn chế sau

  • Bạn không thể thêm đầu trang hoặc chân trang, kể cả số trang, vào tài liệu
  • Ví dụ, các tùy chọn in cho tài liệu HTML không bao gồm khả năng in các phạm vi trang. In trang 2 đến 4 của tài liệu HTML 10 trang không được hỗ trợ
  • Phiên bản của
    private WebView mWebView;
    
    private void doWebViewPrint() {
        // Create a WebView object specifically for printing
        WebView webView = new WebView(getActivity());
        webView.setWebViewClient(new WebViewClient() {
    
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    return false;
                }
    
                @Override
                public void onPageFinished(WebView view, String url) {
                    Log.i(TAG, "page finished loading " + url);
                    createWebPrintJob(view);
                    mWebView = null;
                }
        });
    
        // Generate an HTML document on the fly:
        String htmlDocument = "

    Test Content

    Testing, " + "testing, testing...

    "; webView.loadDataWithBaseURL(null, htmlDocument, "text/HTML", "UTF-8", null); // Keep a reference to WebView object until you pass the PrintDocumentAdapter // to the PrintManager mWebView = webView; }
    8 chỉ có thể xử lý một lệnh in tại một thời điểm
  • Không hỗ trợ tài liệu HTML chứa thuộc tính in CSS, chẳng hạn như thuộc tính ngang
  • Bạn không thể sử dụng JavaScript trong tài liệu HTML để kích hoạt in

Ghi chú. Nội dung của một đối tượng

private WebView mWebView;

private void doWebViewPrint() {
    // Create a WebView object specifically for printing
    WebView webView = new WebView(getActivity());
    webView.setWebViewClient(new WebViewClient() {

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                Log.i(TAG, "page finished loading " + url);
                createWebPrintJob(view);
                mWebView = null;
            }
    });

    // Generate an HTML document on the fly:
    String htmlDocument = "

Test Content

Testing, " + "testing, testing...

"; webView.loadDataWithBaseURL(null, htmlDocument, "text/HTML", "UTF-8", null); // Keep a reference to WebView object until you pass the PrintDocumentAdapter // to the PrintManager mWebView = webView; }
8 được bao gồm trong bố cục cũng có thể được in sau khi nó đã tải tài liệu

Nếu bạn muốn tạo bản in đầu ra tùy chỉnh hơn và có toàn quyền kiểm soát nội dung vẽ trên trang in, hãy chuyển sang bài học tiếp theo. In một bài học tài liệu tùy chỉnh

Tạo lệnh in

Sau khi tạo một

private WebView mWebView;

private void doWebViewPrint() {
    // Create a WebView object specifically for printing
    WebView webView = new WebView(getActivity());
    webView.setWebViewClient(new WebViewClient() {

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                Log.i(TAG, "page finished loading " + url);
                createWebPrintJob(view);
                mWebView = null;
            }
    });

    // Generate an HTML document on the fly:
    String htmlDocument = "

Test Content

Testing, " + "testing, testing...

"; webView.loadDataWithBaseURL(null, htmlDocument, "text/HTML", "UTF-8", null); // Keep a reference to WebView object until you pass the PrintDocumentAdapter // to the PrintManager mWebView = webView; }
8 và tải nội dung HTML của bạn, ứng dụng của bạn gần như đã hoàn thành một phần của quy trình in. Các bước tiếp theo là truy cập
webView.loadDataWithBaseURL(
        "file:///android_asset/images/",
        htmlBody,
        "text/HTML",
        "UTF-8",
        null
)
7, tạo bộ điều hợp in và cuối cùng là tạo lệnh in. Ví dụ sau minh họa cách thực hiện các bước này

Kotlin

private WebView mWebView;

private void doWebViewPrint() {
    // Create a WebView object specifically for printing
    WebView webView = new WebView(getActivity());
    webView.setWebViewClient(new WebViewClient() {

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                Log.i(TAG, "page finished loading " + url);
                createWebPrintJob(view);
                mWebView = null;
            }
    });

    // Generate an HTML document on the fly:
    String htmlDocument = "

Test Content

Testing, " + "testing, testing...

"; webView.loadDataWithBaseURL(null, htmlDocument, "text/HTML", "UTF-8", null); // Keep a reference to WebView object until you pass the PrintDocumentAdapter // to the PrintManager mWebView = webView; }
5

Java

private WebView mWebView;

private void doWebViewPrint() {
    // Create a WebView object specifically for printing
    WebView webView = new WebView(getActivity());
    webView.setWebViewClient(new WebViewClient() {

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                Log.i(TAG, "page finished loading " + url);
                createWebPrintJob(view);
                mWebView = null;
            }
    });

    // Generate an HTML document on the fly:
    String htmlDocument = "

Test Content

Testing, " + "testing, testing...

"; webView.loadDataWithBaseURL(null, htmlDocument, "text/HTML", "UTF-8", null); // Keep a reference to WebView object until you pass the PrintDocumentAdapter // to the PrintManager mWebView = webView; }
6

Ví dụ này lưu một thể hiện của đối tượng

webView.loadDataWithBaseURL(
        "file:///android_asset/images/",
        htmlBody,
        "text/HTML",
        "UTF-8",
        null
)
8 để ứng dụng sử dụng, điều này không bắt buộc. Ứng dụng của bạn có thể sử dụng đối tượng này để theo dõi tiến trình của lệnh in khi nó đang được xử lý. Phương pháp này hữu ích khi bạn muốn theo dõi trạng thái của lệnh in trong ứng dụng của mình để hoàn thành, thất bại hoặc hủy bỏ của người dùng. Không cần tạo thông báo trong ứng dụng vì khung in sẽ tự động tạo thông báo hệ thống cho lệnh in

Chúng tôi có thể sử dụng HTML trong Android Studio không?

HTML xử lý các chuỗi HTML thành văn bản theo kiểu có thể hiển thị và sau đó chúng tôi có thể đặt văn bản trong TextView của mình. Chúng tôi cũng có thể sử dụng WebView để hiển thị nội dung HTML. Hiện tại Android không hỗ trợ tất cả các thẻ HTML nhưng nó hỗ trợ tất cả các thẻ chính

Làm cách nào để tạo tệp HTML trên thiết bị di động?

Làm cho trang web HTML của bạn phù hợp với thiết bị di động .
Bước 1. Một số minh họa. .
Bước 2. Công cụ bạn cần. .
Bước 3. Thêm một dòng mã vào tệp HTML. .
Bước 4. Truy vấn phương tiện cho thiết bị di động. .
Bước 5. Sử dụng các đơn vị động để giữ cho trang web đáp ứng. .
Bước 6. Xác định cấu trúc CSS mới cho các liên kết điều hướng

Làm cách nào để đặt nội dung HTML trong Android?

Đây là giải pháp đơn giản để hiển thị HTML trong TextView trong android. Bước 1 – Tạo một dự án mới trong Android Studio, đi tới Tệp ⇒ Dự án mới và điền vào tất cả các chi tiết cần thiết để tạo một dự án mới. Bước 2 − Thêm đoạn mã sau vào res/layout/activity_main. xml .