📅  最后修改于: 2023-12-03 14:41:18.535000             🧑  作者: Mango
force_dark_auto
Android WebView - Javaforce_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'
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.
force_dark_auto
provides some custom options to help developers adjust the effect. Here are some common options:
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();
By adding the disable
option, you can disable the conversion effect, as follows:
ForceDarkAuto.with(webView)
.disable()
.apply();
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.
force_dark_auto
is a lightweight library that enables the dark theme in Android WebView and improves the user's reading experience.