繁体   English   中英

如何隐藏特定 webview url 的线性布局工具栏?

[英]How to hide Linear Layout toolbar for specific webview url?

我在与相对布局 webview 关联的线性布局中创建了一个工具栏。 如何为特定 url 隐藏此工具栏,例如:'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>

在您的 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 下调用它。

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

}

您需要创建一个函数,它将隐藏一个工具栏。 并为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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM