我正在使用ViewPager开发一个简单的应用程序。它在webview中包含内容和图像。 ViewPager有许多页面,所有页面都有相同的数据。
content.xml中:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<android.support.v4.view.ViewPager
android:id="@+id/content_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="5dp"
android:paddingLeft="9dp"
android:paddingRight="9dp"
android:paddingTop="5dp" />
</RelativeLayout>
SampleActivity.java: 包com.android.sample;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.SimpleOnPageChangeListener;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;
public class SampleActivity extends Activity {
public static ViewPager contentPager;
public static ContentPagerAdapter contentPagerAdapter;
public static List<String> mPages;
public static SampleActivity context;
int _position;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content);
context = this;
mPages = new ArrayList<String>();
for(int i =0;i<20;i++) {
mPages.add("<html><head><meta charset=\"utf-8\"></head><html><head><style type=\"text/css\"> img { max-width:100%; max-height:100%; } </style></head><body><p> "+ i +" <br> <img alt=\"\" class=\"alignleft\" height=\"160\" src=\"http://yourstory.in/wp-content/uploads/2011/10/vakil_search_n.jpg\" style=\"margin: 5px;\" title=\"vakil search\" width=\"280\" /><em>Lawyers at </em><a href=\"http://yourstory.in/?s=vakilsearch\" target=\"_blank\"><em>VakilSearch </em></a><em>shed some light</em></p><p>The categorisation of an expense as revenue expenditure or capital expenditure has been a perpetual ground for litigation between assessees and the authorities.</p>");
}
contentPager = (ViewPager) findViewById(R.id.content_pager);
contentPagerAdapter = new ContentPagerAdapter();
contentPager.setAdapter(contentPagerAdapter);
contentPager.setOnPageChangeListener(new PageListener());
contentPager.setCurrentItem(0 );
}
class PageListener extends SimpleOnPageChangeListener {
public void onPageScrollStateChanged (int state){
//Log.d(TAG, "onPageScrollStateChanged state " + state+ " _position "+_position + "webView"+webView);
}
public void onPageScrolled (int position, float positionOffset, int positionOffsetPixels){
//Log.d(TAG,"onPageScrolled Target webViewPrevious : " + webViewPrevious + " webView " + webView );
}
public void onPageSelected(int position) {
_position = position;
// TextView pageNumberView = (TextView) context.findViewById(R.id.page_no);
//pageNumberView.setText("" + (position + 1) + " of " + (viewNumber));
}
}
public class ContentPagerAdapter extends PagerAdapter {
String html="";
WebView webView;
@Override
public void destroyItem(View collection, int position, Object view) {
//clearALlVedios();
((ViewPager) collection).removeView((RelativeLayout) view);
}
@Override
public void finishUpdate(View arg0) {
}
@Override
public int getCount() {
return mPages.size();
}
@Override
public Object instantiateItem(View collection, int position) {
RelativeLayout ll = new RelativeLayout(context);
webView = new WebView(context);
String content;
try {
content = mPages.get(position) + "<br> ";
}
catch (Exception e) {
content="";
}
webView.clearCache(true);
webView.getSettings().setJavaScriptEnabled(true);
/// webView.getSettings().setLoadWithOverviewMode(false);
webView.getSettings().setPluginsEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.requestFocusFromTouch();
webView.setWebChromeClient(new WebChromeClient());
//webView.getSettings().setUseWideViewPort(false);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.setWebViewClient(new WebViewClient());
// webView.getSettings().setUseWideViewPort(true);
//webView.getSettings().
/*int scale = ((int) (100 * ( webView.getScale()))) + 10 ;
Log.i(TAG, "zoom scale : "+ scale);
webView.setInitialScale( scale );*/
// webView.setVerticalScrollBarEnabled(false);
// webView.setHorizontalScrollBarEnabled(false);
//webView.setInitialScale(140);
html = "<p >"+ content+ " </p>";
ll.setPadding(8, 8, 2, 8);
final String mime = "text/html";
final String encoding = "utf-8";
webView.loadDataWithBaseURL(null, html, mime, encoding, null);
webView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null && ((url.startsWith("http://")) || (url.startsWith("https://")) || (url.startsWith("www.")))) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
} else {
return false;
}
}
});
ll.addView(webView);
((ViewPager) collection).addView(ll);
ll.setTag(position);
return ll;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View arg0) {
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
}
html具有相同结构的不同类型的内容(文本+图像)......
问题:活动开始时,不显示图像。当我点击放大时,图像会显示,当我点按缩小时,图像不会显示。
我搜索过,但我没有找到任何帮助。
答案 0 :(得分:0)
首先,我在代码中看不到setContentView
。 WebView
是另一种视图,例如ImageView
,TextView
..因此您需要使用setContentView
函数。
请注意,您正在使用不同的客户WebViewClient()
和WebChromeClient()
,您需要了解它们之间的区别,主要区别在于WebViewClient
更具可配置性。
您可以至少以两种方式加载网页内容。
第一个是“使用'数据'方案URL将给定数据加载到WebView
”(使用函数
webView.loadData(html, mime, encoding);
另一种方法是直接加载网页的URL。首先,我建议你做一个普通的网页,把它保存在资产文件夹或SD卡中,然后从那里加载。
像这样:
wview.loadUrl("file:///mnt/sdcard/yourfolder/yourpage.html");
wview.loadUrl("file:///android_asset/yourpage.html");
希望,这有点帮助。