2015-02-06 22 views
3

我真的很新的android和我想做一個簡單的應用程序,其中一個視圖顯示全尺寸的web視圖。 我已經啓動了一個帶有導航抽屜的新項目,並添加了僅顯示文本字段的四個菜單。其中一個菜單選項卡稱爲ECG。當我進入菜單並單擊時,我希望此選項卡的視圖以全屏顯示webview - 而不是在瀏覽器中顯示。 我已經在xml文件中創建了一個webview,但我無法弄清楚如何在該視圖中呈現網站。Android Studio,在新的活動中顯示完整的WebView

我希望你們能幫助我!提前致謝!

我MainActivity.java:

package com.nybroe.blivredder; 

import android.app.Activity; 
import android.support.v7.app.ActionBarActivity; 
import android.support.v7.app.ActionBar; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.support.v4.widget.DrawerLayout; 
import android.webkit.WebView; 

public class MainActivity extends ActionBarActivity 
    implements NavigationDrawerFragment.NavigationDrawerCallbacks { 

private NavigationDrawerFragment mNavigationDrawerFragment; 
private CharSequence mTitle; 


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

    mNavigationDrawerFragment = (NavigationDrawerFragment) 
      getSupportFragmentManager().findFragmentById(R.id.navigation_drawer); 
    mTitle = getTitle(); 
    mNavigationDrawerFragment.setUp(
      R.id.navigation_drawer, 
      (DrawerLayout) findViewById(R.id.drawer_layout)); 
} 

@Override 
public void onNavigationDrawerItemSelected(int position) { 

    Fragment objFragment = null; 

    switch (position) { 
     case 0: 
      objFragment = new FrontPage(); 
      break; 
     case 1: 
      objFragment = new ECG(); 
      break; 
     case 2: 
      objFragment = new Electrodes(); 
      break; 
     case 3: 
      objFragment = new Arrhythmia(); 
      break; 

    } 

    FragmentManager fragmentManager = getSupportFragmentManager(); 
    fragmentManager.beginTransaction() 
      .replace(R.id.container, objFragment) 
      .commit(); 
} 

public void onSectionAttached(int number) { 
    switch (number) { 
     case 0: 
      mTitle = getString(R.string.title_section0); 
      break; 
     case 1: 
      mTitle = getString(R.string.title_section1); 
      break; 
     case 2: 
      mTitle = getString(R.string.title_section2); 
      break; 
     case 3: 
      mTitle = getString(R.string.title_section3); 
      break; 
    } 
} 

public void restoreActionBar() { 
    ActionBar actionBar = getSupportActionBar(); 
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); 
    actionBar.setDisplayShowTitleEnabled(true); 
    actionBar.setTitle(mTitle); 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    if (!mNavigationDrawerFragment.isDrawerOpen()) { 
     getMenuInflater().inflate(R.menu.main, menu); 
     restoreActionBar(); 
     return true; 
    } 
    return super.onCreateOptionsMenu(menu); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    int id = item.getItemId(); 

    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

public static class PlaceholderFragment extends Fragment { 
    private static final String ARG_SECTION_NUMBER = "section_number"; 

    public static PlaceholderFragment newInstance(int sectionNumber) { 
     PlaceholderFragment fragment = new PlaceholderFragment(); 
     Bundle args = new Bundle(); 
     args.putInt(ARG_SECTION_NUMBER, sectionNumber); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    public PlaceholderFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_main, container, false); 
     return rootView; 
    } 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
     ((MainActivity) activity).onSectionAttached(
       getArguments().getInt(ARG_SECTION_NUMBER)); 
    } 
} 

} 

我ECG.java

package com.nybroe.blivredder; 

import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.webkit.WebView; 

public class ECG extends Fragment{ 
WebView web_view; 
View rootview; 
@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    rootview = inflater.inflate(R.layout.ecg_main, container, false); 
    return rootview; 
} 


} 

我ecg_main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" android:layout_height="match_parent"> 
<WebView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/webView1"> 
    </WebView> 
</RelativeLayout> 
+1

嗨,老兄,我從你的問題中獲益。謝謝 – 2015-10-20 06:59:16

回答

3

試試這個:

public class ECG extends Fragment{ 
    WebView web_view; 
    View rootview; 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     rootview = inflater.inflate(R.layout.ecg_main, container, false); 
     web_view = (WebView) rootview.findViewById(R.id.webView1); 
     return rootview; 
    } 
} 

,並在XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" android:layout_height="match_parent"> 
<WebView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/webView1"> 
    </WebView> 
</RelativeLayout> 
+0

謝謝胡安,但我在哪裏定義loadURL方法?在web_view下面? – 2015-02-06 18:45:50

+1

是的: web_view.loadUrl(url); 並且不要忘記在清單中添加此權限: 2015-02-06 19:16:18

+0

謝謝!那樣做了。 – 2015-02-06 22:23:38