如何将我的网站转换为Android应用程序

问题描述:

我制作了一个网站,现在我想将其转换为应用程序。如何将我的网站转换为Android应用程序

我有字面上没有知识的Java或Android应用程序。我google了一下,但是,我仍然困惑从哪里开始?

我不是问任何人为我编码。我只想知道,我可以通过哪些步骤实现我的目标。我应该从哪里开始?

请告诉我,什么是步骤。也许,我在Google上搜索错误的关键字。

有没有开源或免费的项目,可以帮助我做到这一点?

+1

你可以去混合应用程序的其余部分(PhoneGap的,离子等) – akhilesh0707

+0

如果你做你的网站在AngularJS或角度,你可以迁移到离子, – Lcop

+0

HTML, CSS,JS,PHP ...是的,我想混合应用程序。 –

使用网页视图来显示你的网站在Android应用程序

+0

我认为OP应该尝试一次这个答案。它只需要10到15分钟来实施和检查这个过程。 –

+0

是的,但这是我的问题,如何实现它?我会尝试使用Phonegap,并通知您,如果我遇到任何问题,谢谢:) –

1.基本用法

集成在您的应用程序将不会超过两步的WebView。首先,您需要在您的xml布局中包含WebView元素。

<WebView 
    android:id="@+id/webView" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"/> 

第二,你已经从你的活动加载webview中的具体URL。下面将谷歌的主页加载到网页视图中。

WebView webView = (WebView) findViewById(R.id.webView); 
webView.loadUrl("YOUR WEBSITE LINK HERE"); 

即使加载一个简单的URL似乎很容易,定制的WebView需要通过的WebView,它是提供方法透彻的认识。我们将从WebView提供的基本方法开始,稍后我们将构建一个简单的浏览器活动,它充当应用程序内浏览器,提供向后,向前和书签选项。我们将通过在Android Studio中启动一个简单的项目逐个学习。

2.创建新项目

  1. 从文件创建Android Studio中一个新的项目通过填充所需的细节⇒新建项目。

  2. 由于我们需要提出网络请求,我们需要在AndroidManifest.xml中添加INTERNET权限。

AndroidManifest.xml中

<?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并添加滑翔库支持。这是在CollapsingToolbar中加载图像所必需的。这一步是可选的,但我建议你按照这篇文章。

dependencies { 
    ... 
    // glide 
    compile 'com.github.bumptech.glide:glide:3.7.0' 
} 

4.打开布局文件的主要活动(activity_main.xml中和content_main.xml)和web视图元素。除此之外,我还添加了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> 

content_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 EXAMPLE HERE