【问题标题】:Layout Issue while trying to hide toolbar尝试隐藏工具栏时出现布局问题
【发布时间】:2016-07-11 06:25:17
【问题描述】:

我目前的布局是

Toolbar

Tablayout

Webview

我试图在用户向上滚动时隐藏工具栏,并在用户向下滚动时再次显示它,而 webview 位于 tablayout 下方。我正在关注教程,但在我的情况下,虽然我得到了影响,但工具栏没有完全滚动(即 1/3 仍在视图中),但我无法查看我认为与布局实现有关的 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;
            }
        });

    }


}

【问题讨论】:

  • 尽可能显示您的活动代码。你正试图在 viewpager 中加载 webview 对吗?
  • 是的,我正在尝试在视图寻呼机中加载 web 视图。活动有必要吗? Activity 只是调用加载 fragment.xml(更新)
  • 好吧,我试试。等几分钟。
  • 在您的 AppBarLayout 中添加 android:fitsSystemWindows="true"
  • @UttamPanchasara 没有帮助。

标签: android android-layout webview


【解决方案1】:

以下行添加到您的 NestedScrollView :

 android:fillViewport="true"

【讨论】:

  • 是的,这确实显示了 web 视图,但缺点是现在我无法滚动视图。如果有帮助,我已经更新了包含 webview 的片段
  • 什么不滚动?你想滚动什么?
  • 当我根据您的建议使用android:fillVierport ="true" 时,是的,我现在可以看到 web 视图,但是以前具有滚动属性的 web 视图现在被禁用,此外,我在布局中实现了滑动以刷新也不行。
  • 移除 android:fillVierport ="true"。您需要为 SwipeRefreshLayout 提供 minHeight,因为仅通过滚动 webview 是不可能滚动 webview 和 coordinatorlayout 的。您可以滚动 webview 但不能滚动其父级而不提及一些固定高度。
【解决方案2】:

这成功了。我在布局中添加了这个

<yourpackage.name.NestedWebView 
    android:id="@id/web_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

&这个在java中

https://github.com/takahirom/webview-in-coordinatorlayout/blob/master/app/src/main/java/com/github/takahirom/webview_in_coodinator_layout/NestedWebView.java

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-16
    • 2015-04-29
    • 1970-01-01
    相关资源
    最近更新 更多