gpt4 book ai didi

java - 如何在 webView 中向网站添加 CSS(填充)

转载 作者:行者123 更新时间:2023-11-28 00:21:06 25 4
gpt4 key购买 nike

我一直想知道如何设置 padding-bottom(自定义 CSS)以使用来自 RemoteConfig 的网页(有一个 URL)。我无权访问网页,因此无法直接在源文件中对其进行编辑。

这是我的代码:

public class PlayoffsFragment extends BaseFragment {
private WebView webView;

public static PlayoffsFragment newInstance(Bundle args) {
PlayoffsFragment fragment = new PlayoffsFragment();
fragment.setArguments(args);
return fragment;
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,@Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_playoffs, container, false);
setActionBar();
setViews();
setViewContents();
return rootView;
}

@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
hideCalendarMenuItem();
}

@Override
protected void setActionBar() {
showActionBar();
setContentMarginOff();
changeToolbarBackground(R.color.transparent);
changeToolbarIconsColor(ContextCompat.getColor(getContext(), R.color.white)); }


@Override
protected void setViews() {
webView = rootView.findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setDatabaseEnabled(true);
webView.getSettings().setMinimumFontSize(1);
webView.getSettings().setMinimumLogicalFontSize(1);
}

@Override
protected void setViewContents() {
webView.loadUrl(BuildConfig.WEB_URL);
}

}

最佳答案

好吧,我设法做到了。对于任何想做类似事情的人,这里是我更新的代码:

@Override
protected void setViews() {
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setDatabaseEnabled(true);
webView.getSettings().setMinimumFontSize(1);
webView.getSettings().setMinimumLogicalFontSize(1);
webView.setWebViewClient(new WebViewClient(){

public void onPageFinished(WebView view, String url){
injectCSS();
super.onPageFinished(view, url);
}
});
}

@Override
protected void setViewContents() {
webView.loadUrl(url);
}

private void injectCSS() {
try {
InputStream inputStream = getContext().getAssets().open("styles.css");
byte[] buffer = new byte[inputStream.available()];
inputStream.read(buffer);
inputStream.close();
String encoded = Base64.encodeToString(buffer, Base64.NO_WRAP);
webView.loadUrl("javascript:(function() {" +
"var parent = document.getElementsByTagName('head').item(0);" +
"var style = document.createElement('style');" +
"style.type = 'text/css';" +
"style.innerHTML = window.atob('" + encoded + "');" +
"parent.appendChild(style)" +
"})()");
} catch (Exception e) {
e.printStackTrace();
}
}

我在 Assets 文件夹中创建了 styles.css。

关于java - 如何在 webView 中向网站添加 CSS(填充),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54869365/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com