android - 按钮在 WebView 前不可见(API 19 及其中)

标签 android button webview visible invisible

我有一个 WebView,前面有一个 Button。 Button 在外部浏览器中打开 url,这在 Android API 21 及更高版本中非常有用。但我也在 API 16 中进行测试以覆盖更多设备。 在 API API 19 及以下版本中,按钮没有出现,按钮在它们上方可见并且可以正常工作。 Button 仅在 WebView 获取 URL 时可见。在此之前,Button 是不可见的。

my relevant Java code is:

public void launchWebViewByURL(String url)
    {
        showSpinner();
        Button browserButton = (Button) fragmentView.findViewById(R.id.WebViewButton);
        browserButton.setVisibility(View.VISIBLE);
        webView.loadUrl(url);
    }

my relevant XMK is

<RelativeLayout
        android:id="@+id/WebViewLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_above="@+id/statusIndicators"
        android:layout_below="@+id/statusIndicatorTop">

        <Button
            android:id="@+id/WebViewButton"
            android:text="@string/webViewButton"
            android:layout_width="80dp"
            android:layout_height="50dp"
            android:textSize="7dp"
            android:alpha="0.7"
            android:visibility="invisible"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_marginRight="5dp"
            android:layout_marginBottom="5dp" />

        <WebView
            android:id="@+id/WebView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:requiresFadingEdge="vertical"
            android:fadingEdgeLength="16dp"
            android:background="@color/colorPrimary"
            android:overScrollMode="ifContentScrolls"
            android:layout_alignParentRight="true"
            android:layout_alignParentLeft="true"
            android:visibility="visible" />


        <RelativeLayout
            android:id="@+id/WebViewOverlay"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/black"
            android:visibility="invisible"
            android:alpha="0.5">

            <com.pnikosis.materialishprogress.ProgressWheel
                android:id="@+id/ProgressWheel"
                android:layout_width="@dimen/spinner_dimen"
                android:layout_height="@dimen/spinner_dimen"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                wheel:matProg_barColor="@color/colorAccent"
                wheel:matProg_circleRadius="@dimen/spinner_dimen"
                wheel:matProg_barWidth="@dimen/spinner_breadth"
                wheel:matProg_progressIndeterminate="true"
                />

        </RelativeLayout>

    </RelativeLayout>

当 WebView 像 Button 一样有 Alpha 时,Button 可见但不可触摸。 当 WebView 不可见时,将显示 Button 并正常工作。 有人可以帮助我在“较低”API(如 19 及以下)中将 Button 置于最前面吗?

最佳答案

按钮不监听触摸的原因是因为它被 WebView 消耗,因为 WebView 在您的 Button 前面。当您更改 WebView 的 alpha 时,它仍然在 ViewGroup 中绘制(在您的情况下是 RelativeLayout),并且正在消耗其范围内的所有触摸事件。因此,您的解决方案是将 Button 放在前面。可以考虑阅读 RelativeLayout 的文档了解它是如何工作的。

下面的代码应该可以工作

<RelativeLayout
    android:id="@+id/WebViewLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/statusIndicators"
    android:layout_below="@+id/statusIndicatorTop">

    <WebView
        android:id="@+id/WebView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary"
        android:fadingEdgeLength="16dp"
        android:overScrollMode="ifContentScrolls"
        android:requiresFadingEdge="vertical"
        android:visibility="visible"/>

    <RelativeLayout
        android:id="@+id/WebViewOverlay"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:alpha="0.5"
        android:background="@android:color/black"
        android:visibility="invisible">

        <com.pnikosis.materialishprogress.ProgressWheel
            android:id="@+id/ProgressWheel"
            wheel:matProg_barColor="@color/colorAccent"
            wheel:matProg_barWidth="@dimen/spinner_breadth"
            wheel:matProg_circleRadius="@dimen/spinner_dimen"
            wheel:matProg_progressIndeterminate="true"
            android:layout_width="@dimen/spinner_dimen"
            android:layout_height="@dimen/spinner_dimen"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            />

    </RelativeLayout>

    <Button
        android:id="@+id/WebViewButton"
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="5dp"
        android:layout_marginRight="5dp"
        android:alpha="0.7"
        android:text="@string/webViewButton"
        android:textSize="7dp"
        android:visibility="invisible"/>
</RelativeLayout>

关于android - 按钮在 WebView 前不可见(API 19 及其中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36569010/

相关文章:

c# - asp.net按钮点击客户端和服务器端代码

ios - 单击时如何将我的按钮图像传递给 ImageView ?

android - 文本字段仍然隐藏在键盘后面 - Titanium Android

android - 使用定位插件(Flutter)时出错

java - 如何在运行时更改 while 循环表达式

java - 尝试保存自定义对象数组 Android

android - Kotlin - 高阶函数成本?

android - Appcompat工具栏的后退按钮和选项按钮为黑色

javascript - 我如何处理 webview 中的确认对话框? UWP Windows 10 应用程序 C#

javascript - cocoa WebView : Cannot call Specific Javascript method using `callWebScriptMethod:`