【问题标题】:How to hide Linear Layout toolbar for specific webview url?如何隐藏特定 webview url 的线性布局工具栏?
【发布时间】:2017-10-28 10:03:08
【问题描述】:

我在线性布局中创建了一个与相对布局 web 视图关联的工具栏。如何隐藏特定网址的此工具栏,例如:'index.php' 我的工具栏代码如下 我已经尝试了很多次,但无法使其与给定的答案正常工作,因此使用 xml 布局代码更新代码。

 if (TextUtils.isEmpty(getString(R.string.toolbar))) {
            showToolBar = false;
        }

        if (showToolBar) {

            mSearch = (ImageView) findViewById(R.id.search);
            mAdd = (ImageView) findViewById(R.id.add);
            mProfile= (ImageView) findViewById(R.id.profile);
            mHome= (ImageView) findViewById(R.id.home);
            mSettings= (ImageView) findViewById(R.id.settings);
          //  ImageView mRefresh = (ImageView) findViewById(R.id.refresh);

            mSettings.setOnClickListener(this);
            mHome.setOnClickListener(this);
            mProfile.setOnClickListener(this);
            mSearch.setOnClickListener(this);
            mAdd.setOnClickListener(this);

           // mRefresh.setOnClickListener(this);

        }

        else {
            LinearLayout llToolbarContainer = (LinearLayout) findViewById(R.id.toolbar_footer);
            if (llToolbarContainer != null) {
                llToolbarContainer.setVisibility(View.GONE);
                RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) mAdView.getLayoutParams();
                lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
            }

XML 布局代码,请帮忙

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

    <FrameLayout
        android:id="@+id/webview_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:visibility="invisible"
        tools:context=".universalwebview.MainActivity">
        <WebView
            android:id="@+id/webview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:scrollbars="none" />
    </FrameLayout>


    <LinearLayout
        android:id="@+id/toolbar_footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="#fff"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/home"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="?attr/selectableItemBackground"
            android:clickable="true"
            android:padding="10dp"
            android:src="@drawable/ic_action_home"
            android:tint="@color/tintcolor" />

        <ImageView
            android:id="@+id/search"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="?attr/selectableItemBackground"
            android:clickable="true"
            android:padding="10dp"
            android:src="@drawable/ic_action_search"
            android:tint="@color/tintcolor" />

        <ImageView
            android:id="@+id/add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="?attr/selectableItemBackground"
            android:clickable="true"
            android:padding="10dp"
            android:src="@drawable/ic_action_add"
            android:tint="@color/tintcolor" />

        <ImageView
            android:id="@+id/profile"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="?attr/selectableItemBackground"
            android:clickable="true"
            android:padding="10dp"
            android:src="@drawable/ic_action_profile"
            android:tint="@color/tintcolor" />


        <ImageView
            android:id="@+id/settings"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="?attr/selectableItemBackground"
            android:clickable="true"
            android:padding="10dp"
            android:src="@drawable/ic_action_settings"
            android:tint="@color/tintcolor" />

    </LinearLayout>

    <ImageView
        android:id="@+id/image_splash"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:visibility="visible"
        android:src="@drawable/splash" />
</RelativeLayout>

【问题讨论】:

    标签: java android webview android-linearlayout android-toolbar


    【解决方案1】:

    在您的 MainActivity 代码中,您不能默认将 Showtoolbar bool 变量设置为 false。因为那是您的底部导航实际填充的地方。因此它可能会产生运行时错误。取而代之的是,使您的线性布局当 url 包含“index.php”时隐藏。

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
    
         if(url.contains("index.php") 
          {
           findViewById(R.id.toolbar_footer).setVisibility(View.GONE);
          }
        else
         {
        findViewById(R.id.toolbar_footer).setVisibility(View.VISIBLE);
         }
    }
    

    在你的 WebViewClient 下调用它。

    【讨论】:

      【解决方案2】:

      你需要创建一个函数,它会隐藏一个工具栏。并为WebView 创建自定义WebViewClient

       webView.setWebViewClient(new WebViewClient() {
              @Override
              public void onPageFinished(WebView view, String url) {
                   super.onPageFinished(view, url);
                   if (url.contains("index.php")) {
                       hideToolBar();
                   }                  
              }
       };
      

      类似的东西。不确定您是否需要onPageFinished,但您可以找到更多信息WebViewClient

      【讨论】:

        【解决方案3】:
        1. 获取网址
        2. 使用url.contains("index.php")判断
        3. 设置showToolBar = false;showToolBar = true;
        4. 使用if (showToolBar) {} else {}

        试试这个。

        private boolean showToolBar;
        private LinearLayout llToolbarContainer;
        
        public void initWebView() {
            llToolbarContainer = (LinearLayout) findViewById(R.id.toolbar_footer);
        
            String url = "your_url";
            if (url.contains("index.php")) {
                showToolBar = false;
                llToolbarContainer.setVisibility(View.GONE);
            } else {
                showToolBar = true;
                llToolbarContainer.setVisibility(View.VISIBLE);
            }
        
                mSearch = (ImageView) findViewById(R.id.search);
                mAdd = (ImageView) findViewById(R.id.add);
                mProfile = (ImageView) findViewById(R.id.profile);
                mHome = (ImageView) findViewById(R.id.home);
                mSettings = (ImageView) findViewById(R.id.settings);
                //  ImageView mRefresh = (ImageView) findViewById(R.id.refresh);
        
                mSettings.setOnClickListener(this);
                mHome.setOnClickListener(this);
                mProfile.setOnClickListener(this);
                mSearch.setOnClickListener(this);
                mAdd.setOnClickListener(this);
        
                // mRefresh.setOnClickListener(this);           
        
        }
        

        【讨论】:

        • 确保if (url.contains("index.php")) 。你想显示或消失。我编辑了它。
        • 删除 xml 代码中的 android:visibility="invisible"
        • 你可以再试一次。
        • 仍然..不工作..不知道为什么它不工作
        猜你喜欢
        • 1970-01-01
        • 2016-09-20
        • 2018-09-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-17
        • 1970-01-01
        相关资源
        最近更新 更多