zoukankan      html  css  js  c++  java
  • android webView不简单

    手机屏幕大小非常伤程序猿

    励志成为一名Javaproject师的我。真的被它伤到了,不仅由于webView的强大。并且这个内容适合各样屏幕大小问题。

    想当年苹果project师嘲笑安卓project师们加班在处理屏幕的适配,简直是在反复的工作,没想到多年后,苹果的手机也出现了屏幕大小不一样的,苹果project师哭倒在厕所里,万事真的没有绝对啊!

    1.解析RSS的xml文件
    如今大多数的新闻和杂志都会给大家RSS,我们须要解析它的xml文件。可是假设获取里面的数据时,注意它的标签并不都一样,须要注意。
                            File tempFile = Environment.getExternalStorageDirectory()
    					.getAbsoluteFile();
    			File file = new File(tempFile, (format.format(date) + ".xml"));
    			HttpClient client = new DefaultHttpClient();
    			HttpGet get = new HttpGet(RSS_URL);
    			try {
    				HttpResponse response = client.execute(get);
    				if (response.getStatusLine().getStatusCode() == 200) {
    					InputStream inputStream = response.getEntity().getContent();
    
    					FileOutputStream fos = new FileOutputStream(file);
    					int byteread = 0;
    					byte[] buffer = new byte[1024];
    					while ((byteread = inputStream.read(buffer)) != -1) {
    
    						fos.write(buffer, 0, byteread);
    					}
    					fos.flush();
    					fos.close();
    					inputStream.close();
    				}
    			} catch (ClientProtocolException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
                            list=new ArrayList<Map<String,RssItem>>();
    			SAXReader reader = new SAXReader();
    			try {
    				Document document = reader.read(file); // 通过reader对象的read方法获得document对象
    				Element rootelement = document.getRootElement(); // 获得根节点
    				Element channelelement = rootelement.element("channel");// 获得channel节点
    				Iterator channel_iterator = channelelement.elementIterator();
    				
    				 // 准备第一个模板,将须要改变的时间格式   
    				String pat1 = "EEE, dd MMM yyyy HH:mm:ss Z" ;    
    		        // 准备第二个模板,将提取后的日期数字变为指定的格式    
    		        String pat2 = "yyyy年MM月dd日 HH时mm分" ;
    		        Locale local=Locale.ENGLISH;
    		        SimpleDateFormat sdf1 = new SimpleDateFormat(pat1,local) ;        // 实例化模板对象    
    		        SimpleDateFormat sdf2 = new SimpleDateFormat(pat2) ;        // 实例化模板对象    
    				while (channel_iterator.hasNext()) {
    					//仅仅能放在map里,不能发在对象里
    					Map<String,RssItem>map1=new HashMap<String,RssItem>();
    					Element itemelement = (Element) channel_iterator.next();
    					if ("item".equals(itemelement.getName())) {
    						Iterator item_iterator = itemelement.elementIterator();
    						rssItem = new RssItem();
    						while (item_iterator.hasNext()) {
    							
    							Element itemChild = (Element) item_iterator.next();
    							
    							if ("title".equals(itemChild.getName())) {
    								
    								rssItem.setTitle(itemChild.getStringValue());
    								Log.i("RSS", itemChild.getStringValue());
    								
    							}
    							if ("link".equals(itemChild.getName())) {
    								rssItem.setLink(itemChild.getStringValue());
    								Log.i("RSS", itemChild.getStringValue());
    							}
    							if ("description".equals(itemChild.getName())) {
    								rssItem.setDescription(itemChild
    										.getStringValue());
    								Log.i("RSS", itemChild.getStringValue());
    								if (getImgStr(itemChild.getStringValue())!=null) {
    									//区第一个的url地址
    									rssItem.setImage(getImgStr(itemChild.getStringValue()));
    								}else{
    									rssItem.setImage("");
    								}
    								
    								
    								
    							}
    							if ("pubDate".equals(itemChild.getName())) {
    								
    								Date date1= sdf1.parse(itemChild.getStringValue());
    								String pubDate=sdf2.format(date1);
    								rssItem.setPubDate(pubDate);
    								Log.i("RSS", pubDate);
    							}
    							if ("author".equals(itemChild.getName())) {
    								
    								rssItem.setAuthor(itemChild.getStringValue());
    								Log.i("RSS", itemChild.getStringValue());
    							}
    							
    						}
    						if (rssItem.getImage().equals("")) {
    							
    						}else {
    							map1.put("rssItem", rssItem);
    							list.add(map1);
    						}
    						
    						//feed.addItem(map1);
    						
    						
    					}
    				}
    				
    			} catch (DocumentException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			} catch (ParseException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    			
    			 Message message = new Message();
    			 message.what = 1;
    			 handler.sendMessage(message);
    			 RSS_URL=null;
    		}

    这种写法不要学习,须要把当中一部分写成公共的方法。由于也是在学习。全部后期会都封装成公用的方法。
    2.时间的格式化
    由于RSS里的文章日期格式是:Thu, 13 Aug 2015 11:30:00 +0800。 Calendar时间格式。我们须要的格式是2015年9月1日。

    这个该怎么转换呢?网上找的都是以 new DATE()做为转换对象,这不符合我们实际情况,这时候你须要查询你的jdk文档了,在文档里能够找到他的格式:"EEE, dd MMM yyyy HH:mm:ss Z"。

            String a="Fri, 14 Aug 2015 02:20:10 +0800";//
    	String pat1 = "EEE, dd MMM yyyy HH:mm:ss Z" ;    //EEE, dd MMM yyyy HH:mm:ss Z
            // 准备第二个模板,将提取后的日期数字变为指定的格式    
            String pat2 = "yyyy年MM月dd日 HH时mm分ss秒SSS毫秒" ;  //MOTH  
            //因为local生成非常费时间,提前生成
            Locale local=Locale.ENGLISH;
            SimpleDateFormat sdf1 = new SimpleDateFormat(pat1,local) ;        // 实例化模板对象    
            SimpleDateFormat sdf2 = new SimpleDateFormat(pat2) ;        // 实例化模板对象    
            try {
    			Date date= sdf1.parse(a);
    			System.out.println(sdf2.format(date));
    		} catch (ParseException e) {
    			e.printStackTrace();
    		}
        对于这个应该都会非经常常使用。
    3.手机大小适配
    webView非常强大,可以显示html的内容,这让手机可以解析html,并且到了5.0 webView添加了更大的功能。我须要显示文章的图片,可是图片变会超出屏幕大小,为了能在每一个机子里显示的效果,须要加上这两行代码:
                //是内容与手机适配
    	    bodyHtml.getSettings().setUseWideViewPort(true);
    	    bodyHtml.getSettings().setLoadWithOverviewMode(true);
    这样确实能够把图片变得适合手机大小,可是字体随着图片相同的变小了,因此我们须要把字体调大:
                //设置默认的字体大小
    	    bodyHtml.getSettings().setDefaultFontSize(40);
    这尽管不是最佳解决的方法,应该在RSS里文章的内容写设定好图片的最大大小,但假设在又一次改动img标签,会让图片多的文章载入变得慢。因此仅仅能委曲求全应这个办法,(网上写的大多数方法还是不能解决我的需求。还是在泡上在网上的日子找到解决的办法。该站点做的不错,风格我喜欢的类型,简洁风格)把所有代码显演示样例如以下:
                    Intent itent=getIntent();
    		//文章内容
    		String title=itent.getStringExtra("content");
    		
    		//去除超链接,保留字体
    		String regex="<\s*a.*?

    /a\s*>"; title=title.replaceAll(regex, ""); //加入html标头 String news="<html>"+title+"<html>"; //得到WebView组件 WebView bodyHtml=(WebView)this.findViewById(R.id.wv_paper_content); //设置默认的字体大小 bodyHtml.getSettings().setDefaultFontSize(40); //是内容与手机适配 bodyHtml.getSettings().setUseWideViewPort(true); bodyHtml.getSettings().setLoadWithOverviewMode(true); //可以的调用JavaScript代码 bodyHtml.getSettings().setJavaScriptEnabled(true); bodyHtml.getSettings().setDefaultTextEncodingName("utf-8"); //载入HTML字符串进行显示 bodyHtml.loadDataWithBaseURL(null, news, "text/html", "utf-8", null);

       总算把这个星期碰到的问题和解决的方法写完了。尽管仅仅是简单写下,并且还是学了两个星期的安卓。就開始做项目,可是真的学到了非常多知识,尤其运用到了web的知识,我也相信webApp才是未来的主潮。。。





  • 相关阅读:
    子库存安全性控制
    检查装配件属性
    检查加工费是否有父件
    检查委外货位
    只允许操作外协任务
    检查是否存在工艺路线
    不能取组织ID
    加宽任务号宽度
    采购订单供应商地点必输
    只显示标准采购订单
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/7344313.html
Copyright © 2011-2022 走看看