首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在WebView中添加BottomNavigationView?

如何在WebView中添加BottomNavigationView?
EN

Stack Overflow用户
提问于 2020-12-19 19:37:44
回答 1查看 991关注 0票数 0

Android Java

在我的新应用程序中,我试图制作一个WebView来将我的网站嵌入到一个应用程序中。我希望在我的WebView Widget的每个选项卡上都有一个不同的BottomNavigationView

我有3个选项卡.

  1. 首页
  2. 新冠肺炎追踪器
  3. 仪表板
  • 主页会有我的主页
  • 新冠肺炎追踪者会有我的冠状病毒追踪器网站
  • 仪表板将没有任何WebViews

我已经尝试过的

  • 我用的是RelativeLayout而不是ConstraintLayout
  • 我试过移除片段,但那只是删除了BottomNavigationView
  • 我尝试过添加一个LinearLayout

这里是我的代码

MainActivity.java

代码语言:javascript
复制
package com.example.user.test;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.widget.TextView;
/ * test * /
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
    private WebView myWebView;
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_main);
        WebView myWebView = (WebView) findViewById (R.id.webview1);
        // Cancel standard browser
        myWebView.setWebViewClient (new WebViewClient ());
        // JS
        myWebView.getSettings (). setJavaScriptEnabled (true);
        // USERAGENT
        String userAgent = myWebView.getSettings (). GetUserAgentString ();
        myWebView.getSettings (). setUserAgentString (userAgent + "appwebview");
        // URL read when the app starts
        myWebView.loadUrl ("http://test.net");
    }
    public BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener () {
        @Override
        public boolean onNavigationItemSelected (@NonNull MenuItem item) {
            switch (item.getItemId ()) {
                case R.id.navigation_home:
                    myWebView.loadUrl ("http://test.net/photo");
                    return true;
                case R.id.navigation_dashboard:
                    myWebView.loadUrl ("http://test.net/kiji");
                    return true;
                case R.id.navigation_notifications:
                    myWebView.loadUrl ("http://test.net/");
                    return true;
            }
             return false;
        }
    };
}

Main_Activity.xml

代码语言:javascript
复制
<? xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
    xmlns: app = "http://schemas.android.com/apk/res-auto"
    xmlns: tools = "http://schemas.android.com/tools"
    android: id = "@ + id/container"
    android: layout_width = "match_parent"
    android: layout_height = "match_parent"
    android: orientation = "vertical"
    tools: context = "com.example.user.test.MainActivity">
    <WebView
        android: id = "@ + id/webview1"
        android: layout_width = "match_parent"
        android: layout_height = "wrap_content"
        android: layout_weight = "4.29">
   </WebView>
    <android.support.design.widget.BottomNavigationView
        android: id = "@ + id/navigation"
        android: layout_width = "match_parent"
        android: layout_height = "77dp"
        android: layout_gravity = "bottom"
        android: background = "? android: attr/windowBackground"
        app: menu = "@ menu/navigation" />
<LinearLayout>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-19 20:56:54

您的主要布局应该如下所示:

activity_main.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:background="@android:color/transparent"
        app:menu="@menu/menu" />

    <FrameLayout
        android:id="@+id/fragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/bottomNavigation"
        />
    
</androidx.constraintlayout.widget.ConstraintLayout>

这里有您的底部视图和一个FrameLayout,用于浏览三个片段。

然后,在旅游主活动中,您必须根据单击底部视图加载所需的片段。

代码语言:javascript
复制
public class MainActivity extends AppCompatActivity {

    BottomNavigationView navigation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        navigation = (BottomNavigationView) findViewById(R.id.bottomNavigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }



    private final BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            Fragment fragment;
            switch (item.getItemId()) {
                case R.id.firstFragment:
                    fragment = FragmentWebView.newInstance(urlToLoad);
                    loadFragment(fragment);
                    return true;
                case R.id.secondFragment:
                    fragment = FragmentWebView.newInstance(urlToLoad);
                    loadFragment(fragment);
                    return true;
                case R.id.thirdFragment:
                   fragment = new ThirdFragment.newInstance();
                    loadFragment(fragment);
                    return true;
            }
            return false;
        }
    };

    private void loadFragment(Fragment fragment) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fragmentContainer, fragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }
}

如果您想要在前两个片段上加载web视图,您只能创建一个片段并在newInstance中传递要作为参数加载的url。

代码语言:javascript
复制
fragment = FragmentWebView.newInstance(urlToLoad);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65373745

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档