Andriod使用webview控件往APP里内嵌网页
2023-09-11 14:17:10 时间
1.布局文件片段:res-layout
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.test.webview.MainActivity" tools:ignore="MergeRootFrame" > <WebView android:id="@+id/Toweb" android:layout_width="match_parent" android:layout_height="match_parent" /> </FrameLayout>
2.Java片段:src
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //WebView WebView browser=(WebView)findViewById(R.id.Toweb); browser.loadUrl("http://www.baidu.com"); //设置可自由缩放网页 browser.getSettings().setSupportZoom(true); browser.getSettings().setBuiltInZoomControls(true); // 如果页面中链接,如果希望点击链接继续在当前browser中响应, // 而不是新开Android的系统browser中响应该链接,必须覆盖webview的WebViewClient对象 browser.setWebViewClient(new WebViewClient() { public boolean shouldOverrideUrlLoading(WebView view, String url) { // 重写此方法表明点击网页里面的链接还是在当前的webview里跳转,不跳到浏览器那边 view.loadUrl(url); return true; } }); } //go back @Override public boolean onKeyDown(int keyCode, KeyEvent event) { WebView browser=(WebView)findViewById(R.id.Toweb); // Check if the key event was the Back button and if there's history if ((keyCode == KeyEvent.KEYCODE_BACK) && browser.canGoBack()) { browser.goBack(); return true; } // return true; // If it wasn't the Back key or there's no web page history, bubble up to the default // system behavior (probably exit the activity) return super.onKeyDown(keyCode, event); }
3. AndroidManifest.xml 设置权限,否则无法访问
我们要想将web嵌入到app内我们需要作出xml的配置,打开上面的xml文件,在application节点下方新增这样的配置信息:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
完整的配置文件
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.test.webview" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.test.webview.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.INTERNET"></uses-permission> </manifest>
相关文章
- Android常见App加固厂商脱壳方法的整理
- App.Config Transformation for projects which are not Web Projects in Visual Studio? app.debug.config
- Google Earth Engine APP(GEE)—设定中国区域的一个夜间灯光时序分析app
- Google Earth Engine(GEE)APP——一个监测影像各波段的DN值的app
- ArgoCD 用户管理、RBAC 控制、脚本登录、App 同步
- uni - app - Property or method “toJSON“ is not defined on the instance but referenced during render.
- uni-app - 插件[App云打包]安装失败!(app打包时显示app云打包插件安装失败)解决方案
- ERROR ITMS-90717: “Invalid App Store Icon. The App Store Icon in the asset catalog in ‘HBuilder.app‘
- APP测试面试题汇总(基础篇、进阶篇)
- Sina App Engine与Google App Engine区别对比
- 【3D打印】Ender3 V2刷第三方固件后无法使用创想云APP的解决办法
- App Extension
- uni-app开发中的#ifdef MP是什么意思?条件编译
- 【前端】仿消息推送到App提示
- 【Android】App开发-动画效果篇
- 物联网平台搭建的全过程介绍(五)——基于阿里云物联网平台的Android聊天app源码
- 怎么样android app正在使用STL库
- App开放接口api安全性—Token签名sign的设计与实现
- uni-app的下载