how to center webview contents in android -
screenshot:
hi, there way can center text , image of articles?
this layout file:
<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 code
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 views title.settext(ffeed.getitem(fpos).gettitle()); wb.loaddata(ffeed.getitem(fpos).getdescription(), "text/html; charset=utf-8", "utf-8"); return view; } }
the source code of app available here
thanks
try,
wb.getsettings().setdefaultzoom(zoomdensity.far);
this zoom out , fit image screen , show in center.
note: because webview used render scripts , show in view, long source of web url isn't aligned in center can't change it. can parse web content , show them in customizable way want.
so far the github project talking used same approach. html parser called jsoup used parse html contents , showing in view.
Comments
Post a Comment