gpt4 book ai didi

android - 如何在android中将webview内容居中

转载 作者:行者123 更新时间:2023-11-29 17:35:57 25 4
gpt4 key购买 nike

屏幕截图:

https://camo.githubusercontent.com/7c4b76724b703bcded8aafbba9c012542726462c/687474703a2f2f692e696d6775722e636f6d2f716375554d664a2e706e6725374275726c253744

你好,有什么办法可以让文章的文字和图片居中吗?

这是布局文件:

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">


<RelativeLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:gravity="center">

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text=""
android:textSize="20sp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
</RelativeLayout>

<ScrollView
android:id="@+id/sv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/header" >

<LinearLayout
android:id="@+id/lin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical" >

<WebView
android:id="@+id/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:scrollbarStyle="insideOverlay"
android:text="@string/desc" />
</LinearLayout>
</ScrollView>
</RelativeLayout>

Java 代码

package com.iven.lfflfeedreader.mainact;

import com.iven.lfflfeedreader.R;
import com.iven.lfflfeedreader.domparser.RSSFeed;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.widget.ScrollView;
import android.widget.TextView;


public class ArticleFragment extends Fragment {
private int fPos;
RSSFeed fFeed;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fFeed = (RSSFeed) getArguments().getSerializable("feed");
fPos = getArguments().getInt("pos");
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater
.inflate(R.layout.article_fragment, container, false);
TextView title = (TextView) view.findViewById(R.id.title);
WebView wb = (WebView) view.findViewById(R.id.desc);
ScrollView sv = (ScrollView) view.findViewById(R.id.sv);
sv.setVerticalFadingEdgeEnabled(true);

// Set webview settings
WebSettings ws = wb.getSettings();
ws.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);

// Set the views
title.setText(fFeed.getItem(fPos).getTitle());
wb.loadData(fFeed.getItem(fPos).getDescription(), "text/html; charset=utf-8", "UTF-8");
return view;
}
}

应用程序的源代码在这里可用

谢谢

https://github.com/enricocid/lffl-feed-reader

最佳答案

尝试,

wb.getSettings().setDefaultZoom(ZoomDensity.FAR);

这将缩小并使图像适合您的屏幕并在中心显示。

注意:因为 WebView 用于呈现脚本并将其显示在 View 中,所以只要 web url 的源未居中对齐,您就无法更改它。但是,您可以解析 Web 内容,然后以您想要的任何可自定义方式显示它们。

到目前为止,您所说的github项目也使用了相同的方法。名为 Jsoup 的 html 解析器用于解析 html 内容并在 View 中显示。

关于android - 如何在android中将webview内容居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30562644/

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