Android Studio,在新活动中显示完整的 WebView
Posted
技术标签:
【中文标题】Android Studio,在新活动中显示完整的 WebView【英文标题】:Android Studio, show full WebView in new activity 【发布时间】:2015-04-06 22:58:28 【问题描述】:我是 android 的新手,我正在尝试制作一个简单的应用程序,其中一个视图显示一个全尺寸的 webview。 我已经开始了一个带有导航抽屉的新项目,并添加了四个仅显示文本字段的菜单。其中一个菜单选项卡称为 ECG。当我转到菜单并单击它时,我希望此选项卡的视图以全屏显示 web 视图 - 而不是在浏览器中。 我在 xml 文件中创建了一个 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));
我的心电图.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_ android:layout_>
<WebView
android:layout_
android:layout_
android:id="@+id/webView1">
</WebView>
</RelativeLayout>
【问题讨论】:
嘿伙计,你的问题让我受益匪浅。谢谢 【参考方案1】:试试这个:
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_ android:layout_>
<WebView
android:layout_
android:layout_
android:id="@+id/webView1">
</WebView>
</RelativeLayout>
【讨论】:
谢谢 Juan 但是我在哪里定义 loadURL 方法?就在 web_view 下? 是:web_view.loadUrl(url);并且不要忘记在清单中添加此权限:以上是关于Android Studio,在新活动中显示完整的 WebView的主要内容,如果未能解决你的问题,请参考以下文章
当用户切换到 Android Studio Kotlin 中的其他页面时,如何停止播放音乐?
仅将一部分数据显示到列表视图中并获取新活动 android studio 中的所有值?
android studio 显示中显示的操作栏,但在我的实际应用中没有
带有片段的 Android Studio 导航抽屉。工具栏隐藏在下一个片段活动或页面中