如何在bottomnavigationview中添加webview?

6fe3ivhb  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(347)

android studio-java
在我的新应用程序中,我正在尝试创建一个webview来将我的网站嵌入到一个应用程序中。我想要一个不同的 WebView 在我的每个选项卡上 BottomNavigationView 小装置。
我有三个标签。

covid-19跟踪器
Jmeter 板
主页将有我的主页网站
covid-19追踪器会有我的冠状病毒追踪器网站
Jmeter 板将没有任何 WebViews 我试过的
我用了一个 RelativeLayout 而不是 ConstraintLayout 我试过去掉碎片,但那只是去掉了碎片 BottomNavigationView 我试过添加一个 LinearLayout 这是我的密码
主活动.java

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

主活动.xml

<? 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>
uujelgoq

uujelgoq1#

你的主要布局应该是这样的:
活动\u main.xml

<?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>

在这里你有你的底视图和一个巡演三段的框架布局。
然后在tour main活动中,您必须根据单击的底部视图加载所需的片段。

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视图,则只能生成一个片段,并将要加载的url作为参数传递给newinstance。

fragment = FragmentWebView.newInstance(urlToLoad);

相关问题