【发布时间】:2015-04-13 18:15:21
【问题描述】:
我有一个专为在移动设备上展示而设计的网站。所以我决定使用 Android Studio。
我想显示我的 html 文件,该文件来自服务器,但在应用程序中有本地 css 和 js 文件。换句话说,我只想从主机加载 html 并使用应用程序中存在的 css 更改其样式。我正在使用网络视图。
我该怎么办?使用 iframe 或其他东西?请帮我。请为我提供正确的代码。
我的 MainAtivity.java 文件:
package com.example.z5070.myapplication;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url = "http://www.example.com";
WebView view = (WebView) this.findViewById(R.id.webView);
view.loadUrl(url);
}
@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_main, 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 == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我的activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
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=".MainActivity">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webView"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
/>
【问题讨论】:
-
看看这里:将 CSS 注入到带有 android 中 webview 的站点:stackoverflow.com/questions/30018540/…