【问题标题】:Set Imagebutton on webview在 webview 上设置 Imagebutton
【发布时间】:2017-06-14 19:30:54
【问题描述】:

在我的应用程序中,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> 

【问题讨论】:

    标签: java android webview imagebutton


    【解决方案1】:

    尝试使用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>
    

    这允许按钮在您滚动 web 视图时保持在原位

    编辑:

    您是否尝试过将按钮移出 web 视图?

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

    如果您想要全屏模式,只需将线性布局更改为相对布局:

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

    【讨论】:

    • @th3gr3yman 尝试交换位置,将按钮放在 webview 之后
    • 是一样的
    • @th3gr3yman 检查我的编辑,尝试将按钮移出并将其放在 web 视图上方
    • 使用您的代码,webview 未处于全屏模式
    • @th3gr3yman 检查我的编辑,只需将其从 linearLayou 更改为 RelativeLayout 即可全屏
    猜你喜欢
    • 2016-01-21
    • 1970-01-01
    • 1970-01-01
    • 2013-08-20
    • 2011-05-08
    • 1970-01-01
    • 1970-01-01
    • 2014-02-18
    • 1970-01-01
    相关资源
    最近更新 更多