以编程方式设置WebView尺寸

时间:2019-02-16 09:48:33

标签: android android-layout

我在代码中使用了Webview,并且试图通过编程方式设置尺寸。我有办法吗?

1 个答案:

答案 0 :(得分:3)

您应该改用RelativeLayout

示例:

假设您要在屏幕上位置(70、80)放置大小为(50、60)的WebView

// RelativeLayout. though you can use xml RelativeLayout here too by `findViewById()`

RelativeLayout relativeLayout = new RelativeLayout(this);
// webView
WebView w= new WebView(this);
    w.setId(0X100);
    w.setScrollContainer(false);
// Setting layout params to our RelativeLayout
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(50, 60);

// Setting position of our ImageView
layoutParams.leftMargin = 70;
layoutParams.topMargin = 80;

// Finally Adding the imageView to RelativeLayout and its position
relativeLayout.addView(w, layoutParams);