【问题标题】:How to make tabs with webview to load from a web address?如何使用 webview 制作标签以从网址加载?
【发布时间】:2016-07-16 20:13:49
【问题描述】:

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

| tab one | tab two | tab three |

...每个选项卡都应该加载一个 webview 页面。

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

我该如何构建它?

【问题讨论】:

  • 欢迎如此 ;-) 您需要什么帮助才能准确地构建它?在这里找不到其他人为您做这件事。请编辑您的问题以显示您已经尝试过的内容。
  • @StefanHegny 我想在 android studio 中构建应用程序里面有标签:标签之一 |标签二 |标签三 | .并且每个点击加载 webview 页面例如:标签一个加载 www.google.com 标签拖加载 www.youtube.con
  • 我改写并格式化了您的问题。请添加您尝试过的代码。

标签: java android android-studio android-webview android-tabs


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

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

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

}

与上面创建的一样创建不同的片段。

【讨论】:

    【解决方案2】:

    实际上需要创建 FragmentActivites 在您的布局中,您需要将 SetBackground 作为您的愿望颜色,以便 activity_main 不会与 FragmentLayout 一起加载

    对于 Webview 只需根据自己的需要在每个片段中添加 Webview 并加载 Url

    【讨论】:

    • 谢谢你回答我:你可以为它创建简单的项目???或给我链接??
    【解决方案3】:

    我的代码

    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 的原因。

    【讨论】:

    • 我在写 import.android.app.Fragment 时收到错误
    • 应该编辑您的问题以添加代码,而不是将其发布在答案部分。请阅读此how-to-ask
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多