Android 在 webview 中设置控件的边距不起作用

Android set margins for control inside webview doesn't work

我有一个 webview,一个向这个 webview 添加新按钮的功能,这里的问题是我不能不设置这个按钮的边距,它总是显示在 webview 的顶部,左边。

或者针对我的情况的任何解决方案:我想在 webview 中的指定位置(x,y)添加一个控件,当用户在 webview 上滚动时,该控件也会滚动并始终准确显示我之前设置的位置?

我的xml布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:id="@+id/mainLayout"
   >

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

        android:scrollbars="vertical"
        android:visibility="visible">
    </WebView>
</LinearLayout>

我创建新按钮的代码是:

private void createButton(WebView wv){
        //LinearLayout relativeLayout = (LinearLayout)findViewById(R.id.mainLayout);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(60,60, Gravity.TOP);
        params.setMargins(50, 50, 0, 0);// Doesn't work at all here

        android.widget.Button button = new android.widget.Button(this);
        button.setText("Hello button");

        button.setLayoutParams(params);

        wv.addView(button);
    }

任何帮助将在高级中感谢

您必须使用 WebView.LayoutParams 而不是 LinearLayout.LayoutParams

替换

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(60,60, Gravity.TOP);
params.setMargins(50, 50, 0, 0);

WebView.LayoutParams params = new WebView.LayoutParams(60, 60, 50, 50);