📜  force_dark_auto android webview - Java (1)

📅  最后修改于: 2023-12-03 14:41:18.535000             🧑  作者: Mango

force_dark_auto Android WebView - Java

force_dark_auto是一个用Java编写的基于Android WebView的库,可以自动将网页内容转换为深色主题,为用户提供更好的阅读体验。该库采用最新的Android 10特性,运行时自动检测系统的UI模式并进行适配,同时提供了一些自定义选项,方便开发者调整效果。下面是详细介绍。

安装

在你的项目中添加以下依赖:

implementation 'com.github.username:force_dark_auto:1.0.0'
使用

在XML布局文件中添加一个WebView组件,例如:

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

在Java代码中初始化WebView,如下:

WebView webView = findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());

在初始化完成后,调用以下方法启用深色主题:

ForceDarkAuto.apply(webView);

现在WebView中的内容将被自动转换为深色主题,在深色模式下更加易于阅读。

自定义选项

force_dark_auto提供了一些自定义选项,方便开发者调整效果。下面是常用的选项:

排除元素

通过添加exclude选项,可以排除一些元素不被深色主题转换,以逗号分隔,如下:

ForceDarkAuto.with(webView)
    .exclude("header", "footer")
    .apply();
取消转换

通过添加disable选项,可以取消转换效果,如下:

ForceDarkAuto.with(webView)
    .disable()
    .apply();
字体颜色

通过添加textColor选项,可以修改字体的颜色,如下:

ForceDarkAuto.with(webView)
    .textColor(Color.WHITE)
    .apply();

更多选项请参考源代码文档。

效果演示

效果演示

总结

force_dark_auto是一个轻量级的库,可以轻松地在Android WebView中启用深色主题,提高用户的阅读体验。

# `force_dark_auto` Android WebView - Java

`force_dark_auto` is a Java library based on Android WebView, which can automatically convert webpage content into a dark theme for a better reading experience. The library adopts the latest Android 10 features, automatically detects the system's UI mode at runtime, and provides some custom options for developers to adjust the effect. Below is a detailed introduction.

## Installation

Add the following dependency to your project:

```java
implementation 'com.github.username:force_dark_auto:1.0.0'
Usage

Add a WebView component to the XML layout file, for example:

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

Initialize WebView in Java code, as follows:

WebView webView = findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());

After initialization is complete, call the following method to enable the dark theme:

ForceDarkAuto.apply(wWebView);

Now the content in WebView will be automatically converted to a dark theme, which is easier to read in dark mode.

Custom options

force_dark_auto provides some custom options to help developers adjust the effect. Here are some common options:

Exclude elements

By adding the exclude option, you can exclude some elements from being converted to a dark theme, separated by commas, as follows:

ForceDarkAuto.with(webView)
    .exclude("header", "footer")
    .apply();
Disable conversion

By adding the disable option, you can disable the conversion effect, as follows:

ForceDarkAuto.with(webView)
    .disable()
    .apply();
Font color

By adding the textColor option, you can modify the font color, as follows:

ForceDarkAuto.with(webView)
    .textColor(Color.WHITE)
    .apply();

Refer to the source code document for more options.

Demonstration

Demo

Conclusion

force_dark_auto is a lightweight library that enables the dark theme in Android WebView and improves the user's reading experience.