【问题标题】:How can I get a button to appear below a WebView inside a CoordinatorLayout如何让按钮出现在 CoordinatorLayout 内的 WebView 下方
【发布时间】:2016-01-26 19:50:31
【问题描述】:

我在CoordinatorLayout 内有一个WebView,在WebView 下方有一个Button,但按钮永远不会出现。我也试过LinearLayoutFrameLayout

我尝试使用NestedScrollView 而不是RelativeLayout 并且遇到了同样的问题,即使我可以让它与NestedScrollView 一起使用,我也不能使用NestedScrollView 因为其他issues 它有一个WebView

那么我该怎么做才能让 Button 显示在 WebView 下方而无需任何滚动?

这是我的布局:

<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="com.webviewwithbutton.WebViewWithButton">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay"/>

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

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        >

        <WebView
            android:id="@+id/webview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"></WebView>

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_below="@+id/webview"
            android:text="test"/>

    </RelativeLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email"
        app:layout_anchor="@id/app_bar"
        app:layout_anchorGravity="bottom|end"/>

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

这是我的java代码:

public class WebViewWithButton extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view_with_button);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        WebView view = (WebView) findViewById(R.id.webview);
        view.setWebViewClient(new WebViewClient());
        WebSettings settings = view.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setAllowContentAccess(true);
        settings.setAppCacheEnabled(true);
        settings.setDatabaseEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
        settings.setJavaScriptEnabled(true);
        settings.setSupportZoom(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setBuiltInZoomControls(true);

        settings.setAppCacheEnabled(true);
        settings.setAppCachePath(getCacheDir().getAbsolutePath());
        settings.setDatabaseEnabled(true);
        settings.setSupportMultipleWindows(true);
        settings.setLoadWithOverviewMode(true);
        settings.setUseWideViewPort(true);
        settings.setDomStorageEnabled(true);
        settings.setAllowContentAccess(true);
        settings.setAllowFileAccess(true);
        settings.setSaveFormData(true);

        view.loadUrl("http://vimeo.com");
    }


}

编辑:澄清一下,我想使用CoordinatorLayout。我知道我可以轻松完成此任务,问题仅在于使用CoordinatorLayout 时。

编辑:我的维度文件:

<resources>
    <dimen name="app_bar_height">180dp</dimen>
    <dimen name="fab_margin">16dp</dimen>
    <dimen name="text_margin">16dp</dimen>
</resources>

编辑:Styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>

</resources>

编辑:我应该提一下,我所做的只是使用名为 Scrolling Activity 的 Android Studio 模板之一。

【问题讨论】:

  • 为什么不直接使用 LinearLayout 而不是 RelativeLayout?
  • 我也试过了,同样的问题。
  • @mntgoat - FrameLayout 怎么样?我做到了,它正在工作,看看我的答案。
  • @LinX64 是的,我也试过了。
  • 您测试了哪些 Android 版本的代码?

标签: android webview android-webview android-support-library android-coordinatorlayout


【解决方案1】:

您的代码不起作用,因为WebView 在您加载和 url 时会拉伸到其父高度,因此Button 将不在屏幕上。您只需不用将Button 放在WebView 下,而是将WebView 放在Button 上使用layout_above 属性:

<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"
    android:fitsSystemWindows="true"
    tools:context="com.webviewwithbutton.WebViewWithButton">

    <android.support.design.widget.CoordinatorLayout
        android:layout_above="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/app_bar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/app_bar_height"
            android:fitsSystemWindows="true"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/toolbar_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:contentScrim="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_collapseMode="pin"
                    app:popupTheme="@style/AppTheme.PopupOverlay"/>

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

        <android.support.v4.widget.NestedScrollView
            android:id="@+id/nested_scroll_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <WebView
                android:id="@+id/webview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

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

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/fab_margin"
            android:src="@android:drawable/ic_dialog_email"
            app:layout_anchor="@id/app_bar"
            app:layout_anchorGravity="bottom|end"/>

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

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_alignParentBottom="true"
        android:text="test"/>

</RelativeLayout>

编辑: 我已经找到了如何做到这一点的方法 - 你只需将 CoordinatorLayoutButton 放在 RelativeLayout 中,然后做同样的事情:使用 layout_above 属性将 CoordinatorLayout 放在 Button 上方。它应该按预期工作。只需将我上面的 xml 替换为您的即可。

【讨论】:

  • 这几乎可以工作,只是它涵盖了我工具栏下方的大部分内容。如果我只将RelativeLayout 中的内容替换到我的代码中,那么它就不起作用了。
  • 发布您的styles.xml 然后,需要查看您的AppTheme.AppBarOverlayAppTheme.PopupOverlay
  • 编辑并添加。为了制作这个例子,我所做的只是使用 Android Studio 上的滚动活动模板并将WebView 添加到NestedScrollView 中,当这不起作用时,我尝试了一些其他的东西,例如RelativeLayout 等。跨度>
  • 很高兴为您提供帮助:) 并期待您的赏金:)
  • 是的,就是等 Stack Overflow 让我给,他们说我要等 3 个小时。
【解决方案2】:

试试这个:

<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="com.webviewwithbutton.WebViewWithButton">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme" />

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

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <WebView
                android:id="@+id/webview"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

            <Button
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

        </LinearLayout>

    </ScrollView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email"
        app:layout_anchor="@id/app_bar"
        app:layout_anchorGravity="bottom|end" />

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

【讨论】:

  • 感谢您的回复。两个问题。首先,我不能使用NestedScrollView,直到它和WebView之间的错误被修复,例如,加载vimeo.com并一直到底部,页面不会注意到你一直到到底部,这样它就坐在那里而不加载更多内容。第二个问题,在您的屏幕截图上,按钮似乎覆盖在WebView 的顶部,我需要它在下方。
  • @mntgoat - 如果您发现任何错误或其他任何内容(顺便说一句),您应该将它们报告给code.google。用您真正想要的内容更新答案。
  • 我已经举报了。我不确定该解决方案的作用。我用FrameLayout 试过了,但没有用,没有试过,也没有用。您是否在CoordinatorLayout 中使用它,因为我需要使用它。
  • @mntgoat - 我已经用完整的代码更新了答案。你对那个设计没有任何经验。你可能想看看:saulmm.github.io/mastering-coordinator
  • 感谢您的代码,但我复制了它,它有两个相同的问题。在我一直滚动到页面底部之前,该按钮不会显示,并且页面本身并不知道我已经一直向下滚动。
猜你喜欢
  • 2016-10-07
  • 2012-10-03
  • 1970-01-01
  • 1970-01-01
  • 2019-12-20
  • 2019-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多