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

时间:2015-05-24 17:06:44

标签: android layout webview

我有一个webview,一个为此webview添加新按钮的功能,这里的问题是我无法设置此按钮的边距,它始终显示在webview的左上角。

或者我的情况的任何解决方案:我想在webview中指定位置(x,y)添加一个控件,当用户在webview上滚动时,控件也将滚动并始终显示在正好位置I已经设定过?

我的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);
    }

任何帮助将在高级

中得到感谢

1 个答案:

答案 0 :(得分:0)

您必须使用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);