Android WebView 将文本换行到换行符并防止水平滚动



我正在使用WebView来显示内容,但是,长的内容会产生水平滚动,这是不希望的。当我尝试使用setLoadWithOverviewMode()setUseWideViewPort()等方法时,正如所有其他解决方案所建议的那样,文本缩小得太小而无法阅读。自动调整大小布局算法也会产生相同的结果。 期望的结果是让 html 保留其缩放比例,但换行文本以使其适合屏幕,而不是缩小文本大小。这是我的xml,如果有任何帮助的话:

<ScrollView>
...
<LinearLayout
android:id="@+id/details_fragment_description_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="@dimen/margin_extra_extra_large"
android:background="@color/white"
android:orientation="horizontal"
android:paddingTop="@dimen/padding_extra_extra_large"
android:visibility="visible">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingEnd="@dimen/padding_extra_extra_large"
android:paddingStart="@dimen/padding_extra_extra_large"
android:src="@drawable/ic_drafts_24_biscay"/>
<WebView
android:id="@+id/details_fragment_description_webview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

活动/类中创建WebView的实例,然后在实例上调用setOnTouchListener接口并以编程方式禁用滚动:

webview.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return (event.getAction() == MotionEvent.ACTION_MOVE);
}
}); 

然后,在xml中,用ScrollView包裹WebView并设置android:scrollbars="vertical"

<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical">
<WebView
android:id="@+id/details_fragment_description_webview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</ScrollView>

功劳归于此解决方案

最新更新