我做了一个网站,现在我想把它转换成一个应用程序。
我对 Java 或 Android 应用程序“几乎一无所知”。我用谷歌搜索了一下,但是我仍然很困惑我应该从哪里开始? 我不是要求任何人为我编码。我只是想知道实现目标的步骤。我应该从哪里开始?
请告诉我,步骤是什么。也许,我在谷歌上搜索的关键字有误。
有没有任何开源或免费项目可以帮助我做到这一点?
将 WebView 集成到您的应用程序中只需两个步骤。首先,您需要在 xml 布局中包含 WebView 元素。
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
其次,您已从 Activity 中加载 web 视图中的特定 url。下面将谷歌的主页加载到网络视图中。
WebView webView = (WebView) findViewById(R.id.webView);
webView.loadUrl("YOUR WEBSITE LINK HERE");
尽管加载一个简单的 url 看起来很容易,但自定义 WebView 需要对 WebView 及其提供的方法有透彻的了解。我们将从 WebView 提供的基本方法开始,稍后我们将构建一个简单的浏览器活动,它充当应用程序内浏览器,提供向后、向前和书签选项。我们将通过在 Android Studio 中启动一个简单的项目来一一学习。2。创建新项目
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="info.androidhive.webview" >
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme.NoActionBar" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
3.打开 build.gradle 并添加 Glide 库支持。这是在 CollapsingToolbar 中加载图像所必需的。此步骤是可选的,但我建议您在本文中遵循此步骤。
dependencies {
...
// glide
compile 'com.github.bumptech.glide:glide:3.7.0'
}
4.打开主要活动的布局文件(activity_main.xml 和 content_main.xml)和 WebView 元素。除此之外,我还添加了 CoordinatorLayout、工具栏和 ProgressBar,它们将在加载网页时显示。
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="@dimen/detail_backdrop_height"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:expandedTitleTextAppearance="@android:color/transparent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
</RelativeLayout>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<ProgressBar
android:id="@+id/progressBar"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-7dp"
android:indeterminate="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
内容_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fadeScrollbars="false"
android:scrollbarFadeDuration="0"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.v4.widget.NestedScrollView>
5.现在打开MainActivity.java并修改代码如下。这里的 initCollapsingToolbar() 方法与 WebView 完全无关,但它是在网页向上滚动时提供折叠效果。 Glide 方法用于在工具栏中显示标题图像。
package info.androidhive.webview;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
public class MainActivity extends AppCompatActivity {
private String postUrl = "http://api.androidhive.info/webview/index.html";
private WebView webView;
private ProgressBar progressBar;
private ImageView imgHeader;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
webView = (WebView) findViewById(R.id.webView);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
imgHeader = (ImageView) findViewById(R.id.backdrop);
// initializing toolbar
initCollapsingToolbar();
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(postUrl);
webView.setHorizontalScrollBarEnabled(false);
}
/**
* Initializing collapsing toolbar
* Will show and hide the toolbar txtPostTitle on scroll
*/
private void initCollapsingToolbar() {
final CollapsingToolbarLayout collapsingToolbar =
(CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
collapsingToolbar.setTitle(" ");
AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.appbar);
appBarLayout.setExpanded(true);
// hiding & showing the txtPostTitle when toolbar expanded & collapsed
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
boolean isShow = false;
int scrollRange = -1;
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
if (scrollRange == -1) {
scrollRange = appBarLayout.getTotalScrollRange();
}
if (scrollRange + verticalOffset == 0) {
collapsingToolbar.setTitle("Web View");
isShow = true;
} else if (isShow) {
collapsingToolbar.setTitle(" ");
isShow = false;
}
}
});
// loading toolbar header image
Glide.with(getApplicationContext()).load("http://api.androidhive.info/webview/nougat.jpg")
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imgHeader);
}
}
其余代码WEBVIEW示例在这里