android - 尝试隐藏工具栏时出现布局问题

标签 android android-layout webview

我当前的布局是

Toolbar

Tablayout

Webview

我试图在用户向上滚动时隐藏工具栏,并在用户使用位于选项卡布局下方的 webview 向下滚动时再次显示它。我正在遵循教程,但就我而言,尽管我受到影响,但工具栏未完全滚动(即 1/3 仍在 View 中),而且我无法查看 webview,我认为这与布局实现有关。请更正下面的布局

Appbarmain.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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"
    android:fitsSystemWindows="true"
    tools:context=".Activity.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:layout_scrollFlags="scroll|enterAlways"/>

        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_marginLeft="5dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:tabGravity="fill"
            app:tabMode="scrollable">

        </android.support.design.widget.TabLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>


</android.support.design.widget.CoordinatorLayout>

这里的relativelayout太小,因此我无法查看webview。这是fragment.xml

  <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.NestedScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >

            <ProgressBar
                android:id="@+id/progressBar1"
                style="?android:attr/progressBarStyleSmall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <android.support.v4.widget.SwipeRefreshLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/swipe1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#FFFFFF">

                <WebView
                    android:id="@+id/website_detail_1"
                    android:layout_width="match_parent"
                    android:layout_height="fill_parent"
                    android:numColumns="1"
                    android:focusableInTouchMode="false"
                    android:focusable="false"
                    android:background="#FFFFFF" />
            </android.support.v4.widget.SwipeRefreshLayout>
        </RelativeLayout>

    </android.support.v4.widget.NestedScrollView>

这是包含从 Activity 调用的 webview 的 fragment

public class NewsFragment extends Fragment {



    // private String url;
    private WebView webView;
    private ProgressBar progressBar1;
    private SwipeRefreshLayout mSwipeRefreshLayout1;

    public NewsFragment() {
        // Required empty public constructor
    }


    public static NewsFragment newInstance(String webUrl) {
        //set arguments
        Bundle args = new Bundle();
        args.putString("website", webUrl);
        NewsFragment newsFragment = new NewsFragment();

        newsFragment.setArguments(args);

        return newsFragment;

    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment


        return inflater.inflate(R.layout.fragment, container, false);


    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {

        //get back arguments
        final String url = this.getArguments().getString("website");


        progressBar1 = (ProgressBar) view.findViewById(R.id.progressBar1);

        webView = (WebView) view.findViewById(R.id.website_detail_1);
        webView.getSettings().setJavaScriptEnabled(true);

        webView.setWebChromeClient(new WebChromeClient() {


            public void onProgressChanged(WebView view, int progress) {

                progressBar1.setProgress(progress);

                if (progress == 100) {

                    progressBar1.setVisibility(View.GONE);

                    if (mSwipeRefreshLayout1.isRefreshing()) {
                        mSwipeRefreshLayout1.setRefreshing(false);
                    }
                } else {
                    progressBar1.setVisibility(View.VISIBLE);
                }

            }


        });


        webView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

        });


        webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        WebSettings webSettings = webView.getSettings();
        //   webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        webView.getSettings().setAppCacheEnabled(true);
        webSettings.setDomStorageEnabled(true);

        if (!isNetworkAvailable()) { // loading offline
            webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        }


        webView.loadUrl(url);

        mSwipeRefreshLayout1 = (SwipeRefreshLayout) view.findViewById(R.id.swipe1);
        mSwipeRefreshLayout1.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                webView.loadUrl(url);
            }
        });

        webView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
                    webView.goBack();
                    return true;
                }

                return false;
            }
        });

    }


}

最佳答案

将以下行添加到您的 NestedScrollView 中:

 android:fillViewport="true"

关于android - 尝试隐藏工具栏时出现布局问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38300392/

相关文章:

android - 奇巧杀死: Not allowed to load local resource: file:///android_asset/webkit/android-weberror. png

android - (Android) 将进度条放置在 View 之上

android - 是否可以通过编程方式启用 "Show layout bounds"开发人员选项?

android - 运行时异常 : Width (0) and height (0) cannot be <= 0

Android: 有没有人有webview screen fit 的好方法?

ios - 将数据传输到 WebView

android - Tensorflow-lite - 从量化模型输出中获取位图

android - 展开/折叠 Lollipop 工具栏动画(电报应用程序)

android - "No resource identifier found for attribute ' showAsAction ' in package ' 安卓 '"

android - Android Webview 中的 loadUrl() 因 net::ERR_CACHE_MISS 而失败