问题描述
|
我正在尝试在Web视图的底部放置一个按钮。由于某些原因,它一直显示在顶部。这是我的代码:
webview = new WebView(this);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
Button myButton = new Button(this);
myButton.setId(11);
LayoutParams buttonParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
// params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
myButton.setText(\"A Button!\");
myButton.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));
buttonParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,-1);
myButton.setLayoutParams(buttonParams);
webview.setLayoutParams(params2);
//setBackgroundcolor(0) - You must make the background color transparent before setting backgroundResource
webview.setBackgroundColor(0);
webview.setBackgroundResource(R.drawable.background);
webview.addView(myButton);
编辑-
我将继续在xml中设置可重用视图的基础。我只用ScrollView
里面有RelativeLayout
(包含我的WebView
和Button
)。原因是因为我只想在到达网页末尾时显示ѭ4。
解决方法
WebView
扩展了AbsoluteLayout
,所以您不能使用RelativeLayout.LayoutParams
。此外,不赞成使用“ 7”,而绝对不建议使用“ 3”作为容器。
您可以将ѭ2覆盖在ѭ3之上,然后将它们放置在ѭ13中,它们会堆积起来,并在顶部添加最后一个视图(您的RelativeLayout)。
, WebView
扩展了AbsoluteLayout
,而不是RelativeLayout
,因此为rule2ѭ添加规则不会有任何影响。尝试将按钮的布局重力设置为底部。
, buttonParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE);