gpt4 book ai didi

java - 如何将网页制作成Android应用程序

转载 作者:行者123 更新时间:2023-12-01 22:16:15 26 4
gpt4 key购买 nike

嗨,我正在尝试制作一个基于网络的应用程序,但我是一个初学者,不能 100% 确定我现在在做什么,请指导我,没有错误,但它说应用程序在以下情况下没有响应我运行它

<小时/>

主要Java

<小时/>
    package com.theverge.www.theverge;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import static com.theverge.www.theverge.R.id.*;


public class TheVerge extends Activity {

private WebView the_verge;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_the_verge);

the_verge = (WebView) findViewById(activity_the_verge_webview);
// Enable Javascript
WebSettings webSettings = the_verge.getSettings();
webSettings.setJavaScriptEnabled(true);
// Force links and redirects to open in the WebView instead of in a browser
the_verge.setWebViewClient(new WebViewClient());
the_verge.loadUrl("http://www.theverge.com");
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_the_verge, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}
<小时/>

这是主要 Activity 的 xml

<小时/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"
android:id="@+id/activity_the_verge_webview"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".TheVerge">

<TextView android:text="" android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<小时/>

AndroidManfest xml

<小时/>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.theverge.www.theverge" >

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".TheVerge"
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" />

</manifest>

最佳答案

您需要将 WebView 添加到布局文件中。将其更改为类似这样的内容

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"
android:id="@+id/activity_the_verge_webview"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".TheVerge">

<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />

然后将您的 onCreate() 方法替换为这个方法

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_the_verge);

the_verge = (WebView) findViewById(webView1);
// Enable Javascript
WebSettings webSettings = the_verge.getSettings();
webSettings.setJavaScriptEnabled(true);
// Force links and redirects to open in the WebView instead of in a browser
the_verge.setWebViewClient(new WebViewClient());
the_verge.loadUrl("http://www.theverge.com");
}

关于java - 如何将网页制作成Android应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30955397/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com