Phonegap 原生控件(Android)与html混合

1. 用命令创建cordova项目

cordova coreate hello com.example.hello hello

 

2.打开MainActivity

在onCreate方法中加入

setContentView(R.layout.main_activity);

 

3. main_activity文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<? xml  version="1.0" encoding="utf-8"?>
< LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical" >
 
     < RelativeLayout
         android:layout_width="fill_parent"
         android:layout_height="45dip"
         android:background="#25C28B" >
 
         < ImageButton
             android:id="@+id/cordova_back"
             android:layout_width="wrap_content"
             android:layout_height="match_parent"
             android:background="#00FFFFFF"
             android:paddingLeft="10dp"
             android:paddingRight="20dp"
             android:src="@drawable/back_all" />
 
         < TextView
             android:id="@+id/cordova_title"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_centerInParent="true"
             android:text="原生头部"
             android:textColor="#FFFFFF"
             android:textSize="20sp" />
 
         < Button
             android:id="@+id/btn_report"
             android:layout_width="wrap_content"
             android:layout_height="match_parent"
             android:layout_alignParentRight="true"
             android:background="#00FFFFFF"
             android:paddingLeft="10dp"
             android:paddingRight="20dp"
             android:text="报告"
             android:textColor="#FFFFFF"
             android:textSize="20sp" />
     </ RelativeLayout >
 
     < org.apache.cordova.engine.SystemWebView
         android:id="@+id/cordovaWebView"
         android:layout_width="match_parent"
         android:layout_height="match_parent" />
 
</ LinearLayout >

  SystemWebView是一个扩展的webview

 

4. 重新makeWebView方法和createViews方法

1
2
3
4
5
6
@Override
   protected  CordovaWebView makeWebView() {
       SystemWebView webView = (SystemWebView) findViewById(R.id.cordovaWebView);
       CordovaWebView cordovaWebView =  new  CordovaWebViewImpl( new  SystemWebViewEngine(webView));
       return  cordovaWebView;
   }

  cordovaWebView对于xml里的SystemWebView 控件

 

1
2
3
4
5
6
7
8
9
10
@Override
  protected  void  createViews() {
      if  (preferences.contains( "BackgroundColor" )) {
          int  backgroundColor = preferences.getInteger( "BackgroundColor" , Color.BLACK);
          // Background of activity:
          appView.getView().setBackgroundColor(backgroundColor);
      }
      appView.getView().requestFocusFromTouch();
  }

  

 

5. 效果图

 



本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/p/7088821.html,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值