x1c 0d1x的数据
我的文章细节页面的设计如下:上半部分是原生的,而文章的其余部分(图片,嵌入,tweets.)是作为HTML内容接收的。我想实现字体大小调整功能,为此我使用了mutableIntState
,这会导致标题和摘要在Slider有新值时重新组合。不幸的是,它不会影响webview!尽管我已经创建了一个自定义的webview组合,它接受以下参数:
- FONTSIZE
- html:表示HTML片段的字符串值,稍后我用CSS将其 Package 起来,以给予字体大小。
视图模型:
class PageViewModel: ViewModel(){
//get initial value from sharedPreferences
var fontSize by mutableIntStateOf(Utility.getFontSize())
private set
fun updateFontSize(newValue:Int){
fontSize = newValue
//store new value in shared preferences
Utility.saveFontSize(newValue)
}
}
字符串
MyScreen:
@Composable
fun MyScreen(title: String, summary: String, html: String){
val myViewModel: PageViewModel = viewModel()
Column{
....
//the slider enables the user to select a value ranging between 16f & 26f
Slider(onValueChange = {myViewModel.updateFontSize(it.toInt())},
steps = 3, valueRange= 16f .. 26f, value = myViewModel.fontSize.toFloat())
...
Text(text = title, fontSize = (myViewModel.fontSize + 6).dp)
Text(text = summary, fontSize = myViewModel.fontSize.dp)
MyCustomWebView(html = html, fontSize = fontSize)
}
}
型
我的自定义Webview如下:
@Composable
fun MyCustomWebView(html:String, fontSize: Int){
val context = LocalContext.current
//assuming that html is an html snippet
val htmlStyled = "<html><head><style>body,p,h1,h2,h3,span,a{font-size: ${fontSize}px!important;}</style></head><body>${html}</body></html>"
AndroidView(modifier = Modifier.fillMaxWidth(),
factory = {
WebView(context).apply{
webViewClient = WebViewClient()
settings.cacheMode = WebSettings.LOAD_NO_CACHE
settings.setRenderPriority(WebSettings.RenderPriority.HIGH)
settings.javaScriptEnabled = true
settings.domStorageEnabled = true
settings.allowFileAccess = true
//I even tried using the depricated textSize
//It doesn't update live
/*settings.textSize = when (fontSize) {
16 -> WebSettings.TextSize.SMALLEST
18 -> WebSettings.TextSize.SMALLER
21 -> WebSettings.TextSize.NORMAL
23 -> WebSettings.TextSize.LARGER
else -> WebSettings.TextSize.LARGEST
}*/
loadDataWithBaseURL("", htmlStyled, "text/html", "UTF-8","")
}
}
}
型
任何帮助都非常感谢。谢谢。
1条答案
按热度按时间vc9ivgsu1#
在AndroidView中,必须使用
update
value-parameter..,因此customWebView变为如下:字符串