如何使用Webview制作标签以从网址加载? [英] How to make tabs with webview to load from a web address?

查看:112
本文介绍了如何使用Webview制作标签以从网址加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Android Studio中构建应用程序.我有内部标签:

I want to build an application in Android Studio. I have inside tabs:

| tab one | tab two | tab three |

...,并且每个选项卡都应加载一个Webview页面.

...and each tab should load a webview page.

例如:第一个标签页应加载www.google.com,第二个标签页应加载www.youtube.com,...

For example: tab one should load www.google.com, and tab two should load www.youtube.com, ...

我该如何构建?

推荐答案

在这里

activity_main.xml

activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />

MainActivity.java

MainActivity.java

import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;

public class MainActivity extends Activity {

// Declaring our tabs and the corresponding fragments.
ActionBar.Tab bmwTab, fordTab, toyotaTab;
Fragment bmwFragmentTab = new Fragment();
Fragment toyotaFragmentTab = new Fragment();
Fragment fordFragmentTab = new Fragment();

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

    // Asking for the default ActionBar element that our platform supports.
    ActionBar actionBar = getActionBar();

    // Screen handling while hiding ActionBar icon.
    actionBar.setDisplayShowHomeEnabled(false);

    // Screen handling while hiding Actionbar title.
    actionBar.setDisplayShowTitleEnabled(false);

    // Creating ActionBar tabs.
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Setting custom tab icons.


    // Setting tab listeners.
    bmwTab.setTabListener((ActionBar.TabListener) new Fragment1());
    toyotaTab.setTabListener((ActionBar.TabListener) new Fragment2());
    fordTab.setTabListener((ActionBar.TabListener) new Fragment3());

    // Adding tabs to the ActionBar.
    actionBar.addTab(bmwTab);
    actionBar.addTab(toyotaTab);
    actionBar.addTab(fordTab);
}
}

PagerAdapter.java

PagerAdapter.java

import android.app.ActionBar.Tab;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ActionBar;

public class PagerAdapter implements ActionBar.TabListener {

private Fragment fragment;

// The contructor.
public PagerAdapter (Fragment fragment) {
    this.fragment = fragment;
}

// When a tab is tapped, the FragmentTransaction replaces
// the content of our main layout with the specified fragment;
// that's why we declared an id for the main layout.
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    ft.replace(R.id.activity_main, fragment);
}

// When a tab is unselected, we have to hide it from the user's view.
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    ft.remove(fragment);
}

// Nothing special here. Fragments already did the job.
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {

}
}

activity_fragment1.xml

activity_fragment1.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.ss.tabswithwebview.Fragment1">

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

</WebView>
</RelativeLayout>

Fragment1.java

Fragment1.java

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;

public class Fragment1 extends android.support.v4.app.Fragment {


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.activity_fragment1, container, false);
    WebView webView=(WebView)rootView.findViewById(R.id.webView);
    webView.loadUrl("www.google.com");
    return rootView;
}

}

类似地创建与上面创建的不同片段.

Similarly create different fragment as created above.

这篇关于如何使用Webview制作标签以从网址加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆