【问题标题】:Android Studio, show full WebView in new activityAndroid Studio,在新活动中显示完整的 WebView
【发布时间】: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_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>

【问题讨论】:

  • 嘿伙计,你的问题让我受益匪浅。谢谢

标签: android android-activity webview


【解决方案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_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>

【讨论】:

  • 谢谢 Juan 但是我在哪里定义 loadURL 方法?就在 web_view 下?
  • 是:web_view.loadUrl(url);并且不要忘记在清单中添加此权限:
猜你喜欢
  • 2019-05-22
  • 1970-01-01
  • 2017-05-21
  • 1970-01-01
  • 2016-03-23
  • 2012-06-16
  • 2014-04-02
  • 2018-01-20
  • 1970-01-01
相关资源
最近更新 更多