如何使用webview制作标签以从网址加载?

问题描述 投票:3回答:3

我想在Android Studio中构建一个应用程序。我有里面的标签:

| tab one | tab two | tab three |

...并且每个标签应加载webview页面。

例如:标签1应加载www.google.com,标签2应加载www.youtube.com,...

我怎么能建立这个?

java android android-studio android-webview android-tabs
3个回答
1
投票

这里是

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" />

main activity.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);
}
}

pager adapter.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

 <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

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;
}

}

同样,创建上面创建的不同片段。


0
投票

实际上FragmentActivity需要创建并在你的布局中你需要SetBackground作为你的愿望颜色所以activity_main没有与片段布局一起加载

而对于Webview,只需添加Webview并在每个片段中加载URL根据您自己


0
投票

我的代码

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

public class Adapter implements ActionBar.TabListener {

private android.support.v4.app.Fragment fragment;

// The contructor.
public Adapter(android.support.v4.app.Fragment fragment) {
this.fragment = fragment;
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.activity_main, fragment);
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {

}
}

当点击一个标签时,FragmentTransaction用指定的片段替换主布局的内容;这就是为什么我们为主要布局声明了一个id。

© www.soinside.com 2019 - 2024. All rights reserved.