在webview上设置Imagebutton

时间:2017-06-14 19:30:54

标签: java android webview imagebutton

在我的应用程序中,WebView上有一个ImageButton,但是当我将页面滚动到底部时,它仍然位于顶部。我该如何解决这个问题?

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

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

            <ImageButton
                android:id="@+id/imageButton"
                android:layout_width="88dp"
                android:layout_height="88dp"
                android:layout_x="292dp"
                android:layout_y="450dp"
                android:background="@drawable/downloadarrow" />
        </WebView>    
</LinearLayout> 

1 个答案:

答案 0 :(得分:0)

尝试使用ConstraintLayout

例如:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.test.mytest.MainActivity"
    tools:showIn="@layout/app_bar_main">

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        tools:layout_editor_absoluteX="148dp"
        app:layout_constraintBottom_toBottomOf="@+id/webView1"
        android:layout_marginBottom="8dp" />

    <WebView
        android:id="@+id/webView1"
        android:layout_width="368dp"
        android:layout_height="495dp"
        android:layout_marginBottom="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        tools:layout_editor_absoluteX="8dp" />

</android.support.constraint.ConstraintLayout>

这允许按钮在滚动webview时保持原位

  

修改

您是否尝试过将该按钮移出网络视图?

<LinearLayout android:layout_height="match_parent"
    android:layout_width="fill_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="88dp"
        android:layout_height="88dp"
        android:layout_x="292dp"
        android:layout_y="450dp"
        android:background="@drawable/ic_menu_gallery" />

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

    </WebView>
</LinearLayout>

如果您想要全屏模式,只需将linearlayout更改为relativelayout:

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

    xmlns:android="http://schemas.android.com/apk/res/android">

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

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
</RelativeLayout>